Solidity – Truffle Test with Async/Await

mochasoliditytestingtruffle

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?

Best Answer

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

Related Topic