0

Is that possible to define a function inside a truffle contract test?

There is a repetitive yet complicated step in truffle test scenario I am working on and wondering would it be possible to organize the code and put the repetitive steps to boilerplate function.

My knowledge in ecmascript is limited. All I tried ended up with different errors.

    roundStart(); //TypeError: Cannot read property 'roundStart' of undefined
    this.roundStart(); //TypeError: this.roundStart is not a function
    this.game.roundStart(); //TypeError: Cannot read property 'roundStart' of undefined

Here is the code snippet:

// assume the code runs with no error except the call of roundStart()

contract('SpaceDefense', function ([_, owner, newOwner, player1, player2, hero, stranger]) {

  function roundStart() {
    it('start the round', async function () {
      //..
    });
  });

  describe('pot and pot withdrawal functions by owner', function () {
    it('the pot must match with initial transfer', async function () {
      (await this.game.pot()).should.be.bignumber.equal(100);
    });
    //...

    roundStart(); //TypeError: Cannot read property 'roundStart' of undefined
    this.roundStart(); //TypeError: this.roundStart is not a function
    this.game.roundStart(); //TypeError: Cannot read property 'roundStart' of undefined

    //....
    it('pot withdraws must not allowed during the game', async function () {
      await assertRevert(this.game.potWithdrawTo(1, owner, {from: owner}));
    });
  });
});
  • Truffle uses mocha framework for their test, it is better to make such question in stackoverflow.com – Ismael Oct 31 '18 at 20:30
2

You can use the "before" "beforeEach" "after" and "afterEach" methods to run certain things you need before / after tests.

You can also nest "describe" blocks to a max of 4 levels ( if i remember correctly ), which can be used to describe the current state you are testing.

The idea is to use before blocks for things you want to affect only once, and beforeEach for each and every test that is nested in the current block.

A good idea would be to deploy your contract, for each test you do, before each round, that way you test on a clean contract.

Example:

// define my global variables

let globalVariable;

contract('SpaceDefense', function ([_, owner, newOwner, player1, player2, hero, stranger]) {
    let gameContract;
    beforeEach(async() => {
        // deploy a new game contract
        gameContract = // await deployer.deploy() code
    })


    describe('before round start', function () {
        // use gameContract
    });

    describe('round started', function () {

        // use async here if you want to await for results
        before(async() => {
            // do a function call and assign the result to the global variable
            globalVariable = await this.game.pot();
        })

        // run your tests
        it('the pot must match with initial transfer', async () => {
            (await this.game.pot()).should.be.bignumber.equal(100);
        });

        it('pot withdraws must not allowed during the game', async function () {
            await assertRevert(this.game.potWithdrawTo(1, owner, { from: owner }));
        });
    });

    describe('round ended', function () {

        // use async here if you want to await for results
        before(async() => {
            // do a function call and assign the result to the global variable
            globalVariable = await this.game.pot();
        })

        // run your tests

    });

});
| improve this answer | |
  • Thanks @micky-socaci for your detailed answer. The fundamentals of the contract is that handle 2 states - when the game started and before the next round. Therefor the test steps usually check each feature in both states. It means the state should change between steps and not before/after all. – oliver nadj Oct 31 '18 at 3:47
  • the idea is to describe and test the state in "round started" then in a before block for "round ended" run the methods to move the state along, and test everything again. – Micky Socaci Oct 31 '18 at 16:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.