constetherlime=require('etherlime-lib')constLimeFactory=require('../build/LimeFactory.json');constInterfaceFactory=require('../build/InterfaceFactory.json')constdeployer=newetherlime.EtherlimeGanacheDeployer();constlimeFactory=awaitdeployer.deploy(LimeFactory);//example how to wrap deployed contract and to pass its addressconstcontractInstance=awaitetherlime.ContractAt(InterfaceFactory,limeFactory.contractAddress)
// step1: require Etherlime moduleconstetherlime=require('etherlime-lib')// step2: require compiled contract from ./build,// not the .sol file (as in deployment scripts)constLimeFactory=require('../build/LimeFactory.json')// step4: replace 'contract' descriptor to 'describe', // then remove (accounts) param in async function describe('LimeFactory tests',async () => {// step5: initialize accountlet owner = accounts[0];// step6: set the deployer in before/beforeEach// and fix the deployment scripts as we did beforebeforeEach(asyncfunction() { deployer =newetherlime.EtherlimeGanacheDeployer(owner.secretKey); limeFactory =awaitdeployer.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,
awaitlimeFactory.from(2).createLime('newLime'0,10,12);// as a param you may also use:awaitlimeFactory.from(accounts[1]).createLime('newLime'0,10,12);awaitlimeFactory.from(accounts[1].signer).createLime('newLime'0,10,12);awaitlimeFactory.from(accounts[1].signer.address).createLime('newLime'0,10,12);awaitlimeFactory.from(customSigner).createLime('newLime'0,10,12);
**when you need to execute payable function, pass the value as an
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;awaitassert.revert(limeFactoryInstance.createLime("newLime2", carbohydrates,8,2),"Carbohydrates are not set to 0"); });
test an event
with Truffle:
let expectedEvent ='FreshLime';let result =awaitlimeFactory.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 =awaitlimeFactory.createLime('newLime'8,10,12);consttransactionReceipt=awaitlimeFactory.verboseWaitForTransaction(transaction)// check the transaction has such an eventlet isEmitted =utils.hasEvent(transactionReceipt, LimeFactory, expectedEvent);assert(isEmitted,'Event FreshLime was not emitted');// parse logslet logs =utils.parseLogs(transactionReceipt, LimeFactory, expectedEvent);assert.equal(logs[0].name,'newLime, "LimeFactory" with name "newLime" was not created');