Added expectEvent.inConstructor.

This commit is contained in:
Nicolás Venturo
2018-11-22 13:38:16 -03:00
parent 4469c55358
commit 5e95ef2afc
6 changed files with 88 additions and 3 deletions

View File

@ -1,8 +1,8 @@
const SolidityEvent = require('web3/lib/web3/event.js');
function decodeLogs (logs, contract, address) {
function decodeLogs (logs, events, address) {
return logs.map(log => {
const event = new SolidityEvent(null, contract.events[log.topics[0]], address);
const event = new SolidityEvent(null, events[log.topics[0]], address);
return event.decode(log);
});
}

View File

@ -1,3 +1,5 @@
const { decodeLogs } = require('./decodeLogs');
const BigNumber = web3.BigNumber;
const should = require('chai')
.use(require('chai-bignumber')(BigNumber))
@ -21,6 +23,13 @@ async function inTransaction (tx, eventName, eventArgs = {}) {
return inLogs(logs, eventName, eventArgs);
}
async function inConstruction (contract, eventName, eventArgs = {}) {
const receipt = await web3.eth.getTransactionReceipt(contract.transactionHash);
const logs = decodeLogs(receipt.logs, contract.constructor.events, contract.address);
return inLogs(logs, eventName, eventArgs);
}
function contains (args, key, value) {
if (isBigNumber(args[key])) {
args[key].should.be.bignumber.equal(value);
@ -38,4 +47,5 @@ function isBigNumber (object) {
module.exports = {
inLogs,
inTransaction,
inConstruction,
};

View File

@ -4,11 +4,70 @@ const EventEmitter = artifacts.require('EventEmitter');
const BigNumber = web3.BigNumber;
const should = require('chai')
.use(require('chai-bignumber')(BigNumber))
.use(require('chai-as-promised'))
.should();
describe('expectEvent', function () {
beforeEach(async function () {
this.emitter = await EventEmitter.new();
this.constructionValues = {
uint: 42,
boolean: true,
string: 'OpenZeppelin',
};
this.emitter = await EventEmitter.new(
this.constructionValues.uint,
this.constructionValues.boolean,
this.constructionValues.string
);
});
describe('inConstructor', function () {
context('long uint value', function () {
it('accepts emitted events with correct number', async function () {
await expectEvent.inConstruction(this.emitter, 'LongUint',
{ value: this.constructionValues.uint }
);
});
it('throws if an incorrect value is passed', async function () {
return expectEvent.inConstruction(this.emitter, 'LongUint',
{ value: 23 }
).should.be.rejected;
});
});
context('boolean value', function () {
it('accepts emitted events with correct value', async function () {
await expectEvent.inConstruction(this.emitter, 'Boolean',
{ value: this.constructionValues.boolean }
);
});
it('throws if an incorrect value is passed', async function () {
return expectEvent.inConstruction(this.emitter, 'Boolean',
{ value: !this.constructionValues.boolean }
).should.be.rejected;
});
});
context('string value', function () {
it('accepts emitted events with correct string', async function () {
await expectEvent.inConstruction(this.emitter, 'String',
{ value: this.constructionValues.string }
);
});
it('throws if an incorrect string is passed', async function () {
return expectEvent.inConstruction(this.emitter, 'String',
{ value: 'ClosedZeppelin' }
).should.be.rejected;
});
});
it('throws if an unemitted event is requested', async function () {
return expectEvent.inConstruction(this.emitter, 'UnemittedEvent').should.be.rejected;
});
});
describe('inLogs', function () {