1

I recently upgraded ganache-cli and many of my truffle tests stopped working. In particular, there were tests where I make a contract method invocation with .call() in order to get the return value instead of a transaction back.

For instance, with a method that creates a new thingy and returns the ID that was assigned to it, I used to invoke with 'call()' to get the id back, expecting that no actual data would be persisted. Then I'd make the call 'for real' by not using .call(), and it would yield a transaction. The actual ID assigned would be the one that the invocation with .call() had returned.

My tests are now failing all over the place, and particularly, if I invoke a view function that returns the number of thingies that have been created the number is greater than expected. This is happening within the controlled environment of a unit test running against a local ganache-cli instance.

Can anyone confirm that .call() no longer works as expected and is actually creating data?

  • UPDATE: So, it appears that it isn't the call() function at all. I can run my tests one by one in isolation, and they pass. But If I just run the suite, most of them fail. It seems that the behavior that has stopped working as expected is that truffle test is not redeploying the contracts between tests. – Cliff Hall Nov 24 '18 at 23:17
  • 1
    Which version of truffle/ganache-cli are you using? – ivicaa Nov 25 '18 at 15:11
  • ivicaa - Ganache CLI v6.2.3 (ganache-core: 2.3.1), but that doesn't really matter, as the above update mentions, it seems that the problem is Truffle's test runner failing to redeploy / reset the contracts between test files. – Cliff Hall Nov 26 '18 at 21:59
1

Depending on how you deploy your contracts will determine if Truffle re-deploys them for each test run. If they are deployed in the migration scripts eg 2_deploy_contracts.js they will be deployed only once for your whole test suite. However, if you deploy them in a beforeEach function, described here, inside the contract function in your test file then they should re-deploy for each run. Example from my code here.

Also, consider emitting an event during the function that creates a new thingy, that includes the ID of the new thingy. You can then monitor for this or check the transaction receipt to get the ID rather than executing .call() before submitting a transaction.

  • This answer fixed my problem. For whatever reason, all my tests had been working fine when I used const contract = await MyContract.deployed() but suddenly that behavior had ceased. Now I'm using const contract = await MyContract.new() and all's well. – Cliff Hall Dec 1 '18 at 15:55

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.