0

I have a smart contract with a payable function. My test want to check if that function call will actually increase contract balance, and they apparently works.

it("can present new user", async () => {
    let instance = await MyContract.deployed();
    //presenter is the contract creator
    let presenter = accounts[0];
    let from = accounts[1]; //new user joining 
    await instance.join(presenter,{value:web3.toWei(10,"finney"), from:from});
    let balance = await web3.eth.getBalance(instance.address);
    assert.equal(balance.valueOf(), web3.toWei(10,"finney"));

 })

 it("can't present twice", async () => {
  let instance = await MyContract.deployed();
  //presenter is the contract creator
  let presenter = accounts[0];
  let from = accounts[1]; //new user joining 
  await instance.join(presenter,{value:web3.toWei(10,"finney"), from:from});
  let balance = await web3.eth.getBalance(instance.address);
  assert.equal(balance.valueOf(), web3.toWei(10,"finney"));

})

Then I did another testing function in order to check if calling twice with same parameter disallow the transaction, and it works, but this assumes that:

  1. test inside the same test contract are executed in a certain order
  2. state of all account are committed in the test chain after function call

is this safe to assume?

1

All the "it" test cases inside the same "contract" are executed in the order were they written, and all of them share the same instance of the contract.

For each "contract" truffle will run the migrations scripts to create a new instance of the contracts so previous instances do not affect the results.

Truffle tests are based on Mocha test framework so you can use their documentation to understand how it works.

  • Don't know if it is a good practice to trust in both order and contract instance reuse... – Felice Pollano May 14 '18 at 9:50
  • Mocha is a test framework so they guarantee the test will be run in that order, and truffle tests ensures a new set of contracts is deployed before running a set of tests. It is important for testing that tests are reproducible. But you are correct in that your contract should not assume the functions will always be executed in order, you should have several test each testing different orders. – Ismael May 14 '18 at 15:44

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.