Testing findPlayer()

In a single-testing contract function, it is common to have multiple it blocks instead of combining multiple tests in a single it block. This time, we would like to test the findPlayer() function:

it("..should FIND a player", async () => {
let Cp = await Cplayer.new();
for (let i = 0; i < 3; i++) {
await Cp.addPlayer("player" + i, 123, { from: accounts[i] });
const P = await Cp.findplayer(accounts[i]);
assert.equal(P[0], "player" + i, "player not found");
}
});

This is pretty similar to what we did in the previous test. We only changed the test message and the function we tested.

You may have noticed the redundancy (contract creation) between the two previous it blocks. To remove this inconvenience, we can put this redundant code function into the special beforeEach() function in the contract function:

const [firstAccount, secondAccount, thirdAccount] = accounts;

let Cp;
beforeEach(async function() {
Cp = await Cplayer.new();
});

Consequently, before each test, we instantiate the Cplayer contract, so each test is executed with a clean contract state. We also named three first accounts to avoid using accounts[index]. That way, the test looks cleaner and more elegant.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset