Contract state won't change during Truffle tests

by Craig   Last Updated September 17, 2018 14:28 PM

I am trying to test my contracts using the Truffle suite, along with Ganache.

Up until now, I have been testing contract functions which are restricted to view only. Now, when I attempt to make changes to the contract state, I'm finding that the state is not actually being changed between function calls.

Consider the following simple contract:

pragma solidity ^0.4.24;

import "../OrderBook.sol";

contract TestOrderBook {
    uint256 newId;

    function addId() public returns (uint256) {
        newId++;
        return newId;
    }
}

All I wish to do is increment newId and return its new value.

The code I have written to test this is:

const { getWeb3, getContractInstance, parseSignature } = require("./test_helper");
const web3 = getWeb3();
const getInstance = getContractInstance(web3);

contract('IdManager', (accounts) => {
    let IdManager = getInstance('IdManager');

    it('test newId()', async () => {
        console.log(await IdManager.methods.newId().call());
        console.log(await IdManager.methods.newId().call());
        console.log(await IdManager.methods.newId().call());
    });
});

The getInstance() call is making use of Web3 v1.0, based off this tutorial.

With the above test, I would expect the output to be:

1
2
3

But, I actually get:

1
1
1

Has anyone come across this issue before?



Related Questions


how to test overloaded functoin in truffle?

Updated February 26, 2019 14:28 PM



Truffle Test DeployedAddresses

Updated November 02, 2018 15:28 PM