I reviewed a similar answer but it didn't work. Right now I'm starting with an Example trying truffle, I did a small Echo project based on the Pet Store Box from Truffle. And i'm having some problems:
My contract is like this:
pragma solidity ^0.4.24;
import "./strings.sol";
contract Echo{
address public owner;
string[] private history;
using strings for *;
string public s;
string public lastMessage;
uint public messageCount;
modifier onlyOwner() {
if (msg.sender == owner) _;
}
constructor() public {
owner = msg.sender;
lastMessage = "None yet";
}
function echo(string text) public returns (string) {
history.push(text);
lastMessage = text;
messageCount++;
return text;
}
function fullHistory() public view returns (string){
string memory _history = "";
for (uint i = 0; i < history.length; i++){
_history = _history.toSlice().concat("||".toSlice()).toSlice().concat(history[i].toSlice());
}
return _history;
}
}
So my tests in solidity work perfectly, i received the new values when i check the history of commands:
contract TestEcho {
Echo echo = Echo(DeployedAddresses.Echo());
function testWeReceiveWhatWeSend() public {
string memory testText = "HELLO";
string memory returnedText = echo.echo(testText);
Assert.equal(returnedText, testText, "Should receive the same");
// Assert.notEqual(word_count, 0, "Should have receive more than one");
}
function testWeReceiveTheHistory() public {
string memory testText = "HELLO";
echo.echo(testText);
string memory fullHistory = echo.fullHistory();
Assert.equal(fullHistory, "||HELLO||HELLO", "Should receive something");
}
function testWeReceiveTheLastMessage() public {
string memory testText = "THE SECOND";
echo.echo(testText);
string memory message = echo.lastMessage();
Assert.equal(message, testText, "Should receive the last message");
}
function testWeReceiveTheMessageCount() public {
uint msgCount = echo.messageCount();
Assert.notEqual(msgCount, 0, "Should be more than 0");
}
}
But in javascript, they fail in modifying the Contract State, the history
array won't change ever. The same happens to me in the web. Maybe i'm missing something obvious for sure, but I've been with this for days without a solution:
contract('Echo', async (accounts) => {
const from = accounts[0];
it("should return the same that i sent", async () => {
const instance = await Echo.deployed();
const textMessage = "TEST";
const text = await instance.echo.call(textMessage, {from});
assert.equal(text, textMessage, "echoes");
});
it("should return full history", async () => {
const instance = await Echo.deployed();
await instance.echo.call(web3.fromAscii("TEST"), {from});
await instance.echo.call("TEST", {from});
await instance.echo.call("TEST", {from});
const history = await instance.fullHistory.call();
console.log("FULL_HISTORY", history);
});
}
And for some reason that i do not know, Metamask stop asking me about spending some Gas when i use the echo function in the browser, i already check the port, and the network is correct, the account have funds, the only thing that changed was that i accidently uninstall METAMASK and install a new Version. Still that does not explain why the tests won't work
i will apreciate the help possible
await Echo.deployed()
toawait artifacts.require("Echo.sol").new()
. Also, not a must, but you might want to put it inside abefore
clause instead of inside each one of theit
clauses (depending on how you want your test to behave).