0

This question already has an answer here:

I have the following code in my testtoken.js and am wondering why the assertion fails with "AssertionError: allowance wasn't set to 30: expected '0' to equal 30".

it("Approve an allowance of 30 between act0 and act1", function() {
    return TestToken.deployed().then(function(instance) {
      tt = instance;
      tt.approve.call(accounts[1], 30);
      return tt.allowance.call(accounts[0], accounts[1])
    }).then(function(allowance) {
      assert.equal(allowance.valueOf(), 30, "allowance wasn't set to 30");
    });
  });

This also returns 0 from the command line:

TestToken.deployed().then(function(instance){tt = instance;tt.approve.call(web3.eth.accounts[1], 30);return tt.allowance.call(web3.eth.accounts[0], web3.eth.accounts[1])});

It seems like the state isn't kept after the approval goes through?

EDIT: My fixed test case:

it("Approve an allowance of 30 between act0 and act1", function() {
    return TestToken.deployed().then(function(instance){
      tt = instance;
      tt.approve(web3.eth.accounts[1], 30);
      return tt.allowance.call(web3.eth.accounts[0], web3.eth.accounts[1]).then(function(allowance){
        assert.equal(allowance.valueOf(), 30, "allowance wasn't set to 30");
      })
    });
})

marked as duplicate by Ismael, Community May 29 '18 at 0:20

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    You are making a call, calls do not modify contract storage. You need to create a transaction tt.approve(accounts[1], 30).then(() => tt.allowance.call(accounts[0], accounts[1]); ).then(allowance => console.log(allowance.toNumber())). – Ismael May 28 '18 at 21:41
  • @Ismael you were right! – JonathanScialpi May 29 '18 at 0:20
0

If the contract you are testing is on a real blockchain (ropsten, rinkeby, etc...) and not a test blockchain like Ganache, than it is more than likely that you aren't waiting for the transaction to be mined. You immediately check the allowance of the account after sending the transaction. You will need to wait for the transaction to be mined, before checking.

It is also possible using a language like golang to check pending state data, in which case this would work, however I'm not sure whether or not web3j has something similar to this.

Since you are running your tests on truffle, there is the chance that when you submit your call for the newly updated allowance, that truffle hasn't finished mining the current block. Try adding a 1 second sleep time after submitting the allowance update, before you check the new allowance.

  • I am testing on truffle local environment. Aren't the transactions instant on there? Or are they not to mimic a real blockchain like you described? – JonathanScialpi May 28 '18 at 19:57
  • If that's the case, are you sure that you are actually allowed to make the call? It sounds like you might be hitting a transaction revert error, post your solidity code it may help to debug this. – hextet May 28 '18 at 20:02
  • Even if you are running a local truffle environment, it takes some time (milliseconds) to mine the transaction. Before that transaction is mined, the call to allowance could already have been made, returning the old value – Henk May 28 '18 at 21:02
  • That is a good point as well. Try putting a 1 second sleep before checkin g for the newly updated balance – hextet May 28 '18 at 21:21
  • how can I sleep/wait/pause for a second? – JonathanScialpi May 28 '18 at 23:53

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