1

I'm making test script for my contract and it looks likes

contract('...', async () => {
    let token = null;
    before('deploy', async () => {
        token = await MyContract.new(...);
    });

    it('should success', async() => { ... });
    it('should fail', async() => { ... });    // work as intended only until here
    it('should success', async() => { ... });
});

and executed with command truffle test ./test/test.js

Issue is that calls after second it(...), which contract call fails and invoke revert, never succeed and raise errors similar to

AssertionError: the tx doesn't have the correct nonce. account has nonce of: 37 tx has nonce of: 36

How do I make the test script work? looks like some kind of nonce increment gone wrong, but I'm not sure what I've done wrong. I'm using Ganache GUI 2.0.1 for test network and truffle v5.0.9.

-- Edit --

as comment suggested, I check if I missed any await keyword,

my code is all like this,

it('should deny transfer with insufficient (zero) balance', async () => {
    const amount = web3.utils.toWei('1', 'ether');
    const promise = token.transfer.call(accounts[3], amount, { 'from': accounts[2] });
    await assert.isRejected(promise, 'revert', 'transfer transaction with insufficient (zero) balance must be denied');
});

I tried what Majd TL suggested also,

it('should deny transfer with insufficient (zero) balance', async () => {
    const amount = web3.utils.toWei('1', 'ether');
    const promise = token.transfer(accounts[3], amount, { 'from': accounts[2] });
    await truffleAssert.reverts(promise, 'revert', 'transfer transaction with insufficient (zero) balance must be denied');
});

but it does not resolve the problem. The workaround now is to call contract call that expected to fail with .call, like

const promise = token.nope.call(1, 2, 3, { 'from': accounts[0] });

but in case of really unexpected error, it is inevitable and should be a thing. I Also see this error when using multiple contract(...).

5
  • 1
    I bet you're missing an await somewhere... Commented May 9, 2019 at 8:42
  • what is amount and why it is too big?, also i'm saddly not javascript profi :) but maybe making the whole test in one line could change something, i mean -> await truffleAssert.reverts(token.transfer(accounts[3], 2, .......)
    – Majd TL
    Commented May 13, 2019 at 7:56
  • I think it's not caused by promise, because I managed to make it work by managing nonce per user by hand. So my guess is truffle does not handle nonce when there's errors and manual contract deployment has occurred. and, well I checked every promise I tossed : (
    – xvnm
    Commented May 13, 2019 at 8:23
  • what does the amount value stand for?
    – Majd TL
    Commented May 13, 2019 at 8:52
  • amount value stands for 1 ether, (== 10**18). It seems to big but bignumber type can store and handle it. Also I thought it's not a uncommon to use this large value in token contract.
    – xvnm
    Commented May 13, 2019 at 8:55

3 Answers 3

2
token.transfer(accounts[3], amount, { 'from': accounts[2], nonce: await web3.eth.getTransactionCount(accounts[2]) });

add the nonce parameter will fixed the problem, it work for me

1

Use the truffle assertions library and as goodvibration said don't forget the await

const truffleAssert = require('truffle-assertions');    
...
it('should fail', async function () {
   await truffleAssert.fails(<your_contract>.<your_function>(..., { from: ... }));
});

https://www.npmjs.com/package/truffle-assertions

1
  • thanks for reply, but it could not solve the problem. I replaced my code with your suggestion and check every line if I missed await.
    – xvnm
    Commented May 13, 2019 at 0:41
0

Dear Friend actually truffle developer team has lost control over his project, patching one side creating problem on other side. ( even on their official forum 80% questions are not addressed and in most cases question is for X and reply is for Y ) Now I think they have only one way to start new project with similar goal with fresh and restructured foundation else day be day the project will move in to complexity and impossible to patch bug.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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