5

Has anyone got Truffle tests to work with async/await?

My sample code for testing

require('babel-polyfill');
var ERCToken = artifacts.require("./ERCToken.sol");
var Proxy = artifacts.require("./Proxy.sol");

contract('ERCToken', function(accounts) {
  it("should allow purchase", async function () {
    var expected = 10;
    var meta = ERCToken.deployed();
    var result = await meta.purchase({from:accounts[0],value:80000});
    var balance = await meta.balanceOf(accounts[0]);
    assert.equal(balance.valueOf(),expected,"should have purchased "+ expected + "tokens");
  });
});

TypeError: meta.purchase is not a function

The solidity program does have a function called purchase. ERCToken.deployed() doesn't seem to resolve without a then call.

Can anyone please tell me where am I going wrong?

  • 1
    Actually ERCToken.deployed() always returns only a Promise, while ERCToken.at("address") returns a valid object. – Parthasarathy Ramanujam May 25 '17 at 19:43
  • Please post the answer if you solved it. It's OK to answer your own question. – eth May 26 '17 at 7:21
2

Turned out that the issue was with the way I had deployed the contract using Truffle. Basically Proxy contract's constructor took the address of ERCToken as an input. So Truffle's deployed method resolved correctly only for the main contract (ERCToken) and never for Proxy.

I had to refactor the constructor code by adding separate function to set the ERCToken address and then deployed the two contracts separately. This resulted in both ERCToken.deployed() and Proxy.deployed() resolving correctly.

Completed async/await code available here: https://github.com/zincoshine/solidity-proxy-example

  • I am having this same exact issue but cannot solve it. Can you elaborate further? – blockchaindotsol Sep 15 '17 at 5:25
  • Double check the deployment script that you used to deploy the contract. Is your code available anywhere for me to look? – Parthasarathy Ramanujam Sep 16 '17 at 18:11
  • I seemed to have solved the issue by renaming the contract. I am not sure why this is the case. – blockchaindotsol Sep 17 '17 at 2:02
1

I've used async/await while writing test cases in truffle. You can take a look at the reference code -

https://gist.github.com/inovizz/28908af740f72f94d54f4e4b811da75d

and check this repo for more details.

Hope this helps.

  • Please never "hardlink" to external code, the link here is not working any more - if you have time copy/paste some relevant code-snippets over - I'd be happy to give this an upvote (i browsed your repo already and it did help me - so thanks in advance ;-) ) – olsn Aug 1 '17 at 19:07
  • Yes, we removed this test case as it was not needed. I'll update this with a public gist link, which will not expire. Sorry for the inconvenience. – Sanchit Aug 1 '17 at 19:24

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.