0

below is the test.sol file

pragma solidity ^0.5.0;
contract test {

    string initialstring = "Hello World!";

    function getstring() public view returns(string memory){
        
        return initialstring;

    }

    function setstring(string memory newstring) public  returns(bool success) {
        initialstring = newstring;
        return true;
    }

}

This is the test case written in truffle js

const Test = artifacts.require("test.sol");

contract('Test',(account) => {
    it('Reading Default value', async() => {

        let TestContract = await Test.deployed();
        let orgstring = await TestContract.getstring.call();
        assert.equal("Hello World!", orgstring, "Initial Say Matches");
    });
    it('Inserting new value', async() => {
        let TestContract = await Test.deployed();
        let setstatus = await TestContract.setstring.call("Hi World");
        assert.equal(true,setstatus, "New Value inserted");
        let Newphrase = await TestContract.getstring.call();
        assert.equal("Hi World",Newphrase, "Updated value received");
    });

});

The second test case fails and I am not able to comprehend why it is failing.

0

You use .call() only for contract methods which are not updating the state of the contract ( such method marked with view, pure keywords ).

Try this:

const Test = artifacts.require("test.sol");

contract('Test', (accounts) => {
    it('Inserting new value', async() => {
        return Test.deployed()
        .then(function (instance) {
            return instance.setstring("Hi World", {from: accounts[0]});
        });
    });
});
1
  • Thanks Miroslav it worked now both my test cases are passing – Clyde Arnold Dec 18 '20 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.