// step1: require Etherlime module
const etherlime = require('etherlime-lib')
// step2: require compiled contract from ./build,
// not the .sol file (as in deployment scripts)
const LimeFactory = require('../build/LimeFactory.json')
// step4: replace 'contract' descriptor to 'describe',
// then remove (accounts) param in async function
describe('LimeFactory tests', async () => {
// step5: initialize account
let owner = accounts[0];
// step6: set the deployer in before/beforeEach
// and fix the deployment scripts as we did before
beforeEach(async function() {
deployer = new etherlime.EtherlimeGanacheDeployer(owner.secretKey);
limeFactory = await deployer.deploy(LimeFactory);
});
it('should do something', () => {
})
})
# Flexibility
**in case you want to use an address of an account, you must extend
it to** let owner = accounts[0].signer.address
**when a contract’s method is called, the default sender is set to
accounts[0]. If you want to execute it from another account,
to timeTravel - replace web3 increaseTime with global options
utils.timeTravel(provider, seconds)
Assertions and available utils
For more convenience Etherlime provides some additional assertions and global utils object:
assert it is an address :
it('should be valid address', async () => {
assert.isAddress(limeFactory.contractAddress, "The contract was not deployed");
})
assert a function revert :
it('should revert if try to create lime with 0 carbohydrates', async () => {
let carbohydrates = 0;
await assert.revert(limeFactoryInstance.createLime("newLime2", carbohydrates, 8, 2),
"Carbohydrates are not set to 0");
});
test an event
with Truffle:
let expectedEvent = 'FreshLime';
let result = await limeFactory.createLime('newLime' 8, 10, 12);
assert.lengthOf(result.logs, 1, "There should be 1 event emitted from new product!");
assert.strictEqual(result.logs[0].event, expectedEvent,
`The event emitted was ${result.logs[0].event} instead of ${expectedEvent}`);
with Etherlime
let expectedEvent = 'FreshLime'
let transaction = await limeFactory.createLime('newLime' 8, 10, 12);
const transactionReceipt = await limeFactory.verboseWaitForTransaction(transaction)
// check the transaction has such an event
let isEmitted = utils.hasEvent(transactionReceipt, LimeFactory, expectedEvent);
assert(isEmitted, 'Event FreshLime was not emitted');
// parse logs
let logs = utils.parseLogs(transactionReceipt, LimeFactory, expectedEvent);
assert.equal(logs[0].name, 'newLime, "LimeFactory" with name "newLime" was not created');