javascriptunit-testingsoliditytruffleconsensys-truffle

How to create new instance of contract for whole test suite truffle


I am setting up an ERC20 token ICO on the ethereum network using Solidity and Truffle.

Can anyone tell me why this is not working, I have 4 test files (as I would ideally like to keep them as short and concise as possible), one for testing the token, one for testing the set-up of the crowdsale, one for testing a successful run through (where the cap is reached) and one for testing the emergency stop functionality.

For this reason, I would like to run through multiple it() test cases which use the preceding values to test their own thing in each file i.e.

In the set-up test file:

First it() tests if the contract is deployed

Second it() tests if the contracts variables are set correctly etc.

I have then attempted to create a new instance of the token and contract on each file so that I can run through various test cases.

Each file starts with:

  Token.new(initialAmount, tokenName, decimalUnits,tokenSymbol, {from: account_one});
  Sale.new(softCap, hardCap, etherCostOfEachToken, account_one, token, durationInMinutes, {from: account_one});

However, when running through my tests I get the error:

AssertionError: State of contract was not 'Waiting approval': expected '4' to equal 1
at /Users/jackpickering/Desktop/Development/Capped_ICO/test/4Crowdsale_EmergencyStop_Test.js:28:14
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:228:7)

The status has not been reset back to 1 after finishing the Successful crowdsale test file had ran and therefore, the Sale.new() line must not work.

Is there a better way to do this? Am I missing anything?

Thanks in advance.


Solution

  • The reason I was getting this error was because I was not waiting until the contract had been deployed to the network, instead I changed these to be performed within an asynchronous function and used the 'await' function to wait until the contract had been deployed before carrying on with the rest of the tests.

    My working code is as follows:

    it("should create new token", async function() {
      token = await Token.new(initialAmount, tokenName, decimalUnits,tokenSymbol)
      supply = await token.totalSupply();
      assert.equal(supply, 10000000, "New crowdsale was not created");
    });
    
    it("should create new crowdsale", async function() {
      sale = await Sale.new(softCap, hardCap, etherCostOfEachToken, account_one, Token(token), durationInMinutes);
      state = await sale.getState();
      assert.equal(state, 1, "New crowdsale was not created");
    });