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?

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

2 Answers 2

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

3
  • I am having this same exact issue but cannot solve it. Can you elaborate further? Commented Sep 15, 2017 at 5:25
  • Double check the deployment script that you used to deploy the contract. Is your code available anywhere for me to look? Commented Sep 16, 2017 at 18:11
  • I seemed to have solved the issue by renaming the contract. I am not sure why this is the case. Commented Sep 17, 2017 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.

2
  • 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
    Commented Aug 1, 2017 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
    Commented Aug 1, 2017 at 19:24

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.