diff --git a/test/crowdsale/AllowanceCrowdsale.test.js b/test/crowdsale/AllowanceCrowdsale.test.js index c35707d6d4c..74f26bbf1e4 100644 --- a/test/crowdsale/AllowanceCrowdsale.test.js +++ b/test/crowdsale/AllowanceCrowdsale.test.js @@ -1,10 +1,11 @@ +const expectEvent = require('../helpers/expectEvent'); const { ether } = require('../helpers/ether'); const { assertRevert } = require('../helpers/assertRevert'); const { ethGetBalance } = require('../helpers/web3'); const BigNumber = web3.BigNumber; -const should = require('chai') +require('chai') .use(require('chai-bignumber')(BigNumber)) .should(); @@ -41,12 +42,14 @@ contract('AllowanceCrowdsale', function ([_, investor, wallet, purchaser, tokenW describe('high-level purchase', function () { it('should log purchase', async function () { const { logs } = await this.crowdsale.sendTransaction({ value: value, from: investor }); - const event = logs.find(e => e.event === 'TokensPurchased'); - should.exist(event); - event.args.purchaser.should.equal(investor); - event.args.beneficiary.should.equal(investor); - event.args.value.should.be.bignumber.equal(value); - event.args.amount.should.be.bignumber.equal(expectedTokenAmount); + expectEvent.inLogs( + logs, + 'TokensPurchased', + { + purchaser: investor, + beneficiary: investor, + value: value, + amount: expectedTokenAmount }); }); it('should assign tokens to sender', async function () { diff --git a/test/helpers/expectEvent.js b/test/helpers/expectEvent.js index 37fa1083eb3..b857b6636bf 100644 --- a/test/helpers/expectEvent.js +++ b/test/helpers/expectEvent.js @@ -1,24 +1,18 @@ -const should = require('chai').should(); +const BigNumber = web3.BigNumber; +const should = require('chai') + .use(require('chai-bignumber')(BigNumber)) + .should(); function inLogs (logs, eventName, eventArgs = {}) { const event = logs.find(function (e) { if (e.event === eventName) { - let matches = true; - for (const [k, v] of Object.entries(eventArgs)) { - if (e.args[k] !== v) { - matches = false; - } - } - - if (matches) { - return true; + contains(e.args, k, v); } + return true; } }); - should.exist(event); - return event; } @@ -27,6 +21,20 @@ async function inTransaction (tx, eventName, eventArgs = {}) { return inLogs(logs, eventName, eventArgs); } +function contains (args, key, value) { + if (isBigNumber(args[key])) { + args[key].should.be.bignumber.equal(value); + } else { + args[key].should.be.equal(value); + } +} + +function isBigNumber (object) { + return object.isBigNumber || + object instanceof BigNumber || + (object.constructor && object.constructor.name === 'BigNumber'); +} + module.exports = { inLogs, inTransaction,