ERC20 Approval – Troubleshooting Truffle Test Failures

erc-20testingtruffle

A am implementing a custom ERC20 token inheriting from OpenZeppelin's StandardToken. I override some of the functions but did not override approve or allowance. I write a test for approval:

contract('MyToken', async(accounts) => {
it("approve test", async() => {
    let token = await MyToken.deployed();

    let approveResult = await token.approve.call(accounts[1], 20, {from: accounts[0]});
    assert.isTrue(approveResult);

    let allowanceAccountZeroAccountOne = await token.allowance.call(accounts[0], accounts[1]);
    assert.equal(allowanceAccountZeroAccountOne.toNumber(), 20);
  });
});

I get the following result:

  Contract: MyToken
    1) approve test
    > No events were emitted

  1 failing

  1) Contract: MyToken approve test:
     AssertionError: expected 0 to equal 20
      at Context.it (test/mytoken.js:51:12)
      at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:188:7)

It seems that approve returns true, as expected, but the subsequent allowance returns 0 instead of 20. What could be the problem?

PS I tested the contract manually in Remix, and it works as expected.

Best Answer

Change

let approveResult = await token.approve.call(accounts[1], 20, {from: accounts[0]});

To

let approveResult = await token.approve.sendTransaction(accounts[1], 20, {from: accounts[0]});
Related Topic