用松露测试以太坊事件日志
Test ethereum Event Logs with truffle
我有一个合约函数,它会在每次调用时发出事件。
我想在每个通过的测试上发出一个事件,这里有一些测试:
it("should emit Error event when sending 5 ether", function(done){
var insurance = CarInsurance.deployed();
insurance.send({from: accounts[0], value: web3.toWei(5, 'ether')}).then(done).catch(done);
});
it("should emit Error event when sending 5 ether", function(done){
var insurance = CarInsurance.deployed();
insurance.send({from: accounts[0], value: web3.toWei(5, 'ether')}).then(function(txHash){
assert.notEqual(txHash, null);
}).then(done).catch(done);
});
it("should emit Error event when sending 5 ether", function(done){
var insurance = CarInsurance.deployed();
insurance.send({from: accounts[0], value: web3.toWei(5, 'ether')}).then(function(done){
done();
}).catch(done);
});
结果是:
1) should emit Error event when sending 5 ether
Events emitted during test:
---------------------------
Error(error: Must send 10 ether)
---------------------------
✓ should emit Error event when sending 5 ether (11120ms)
✓ should emit Error event when sending 5 ether (16077ms)
3 passing (51s)
1 failing
1) Contract: CarInsurance should emit Error event when sending 5 ether:
Error: done() invoked with non-Error: 0x87ae32b8d9f8f09dbb5d7b36267370f19d2bda90d3cf7608629cd5ec17658e9b
可以看到只有一个记录失败了。
有什么想法吗?
谢谢
您正在将交易哈希传递给 done() 函数。我认为问题在于:
insurance.send({from: accounts[0], value: web3.toWei(5, 'ether')}).then(done).catch(done);
改为:
insurance.send({from: accounts[0], value: web3.toWei(5, 'ether')}).then(function() { done(); }).catch(done);
测试事件:
it("should check events", function(done) {
var watcher = contract.Reward();
// we'll send rewards
contract.sendReward(1, 10000, {from: accounts[0]}).then(function() {
return watcher.get();
}).then(function(events) {
// now we'll check that the events are correct
assert.equal(events.length, 1);
assert.equal(events[0].args.beneficiary.valueOf(), 1);
assert.equal(events[0].args.value.valueOf(), 10000);
}).then(done).catch(done);
});
从 Truffle v3 开始,您可以在回调结果中获取日志。所以你可以这样做:
insurance.send({from: accounts[0], value: web3.toWei(5, 'ether')}).then((result) => {
assert.equal(result.logs[0].event, "Error", "Expected Error event")
})
见https://github.com/trufflesuite/truffle-contract#processing-transaction-results
有一个帮手可以做到这一点:
npm install --save truffle-test-utils
在测试的顶部:
require('truffle-test-utils').init();
在你的测试中:
let result = await insurance.send({from: accounts[0], value: web3.toWei(5, 'ether')});
assert.web3Event(result, {
event: 'Error',
args: {
error: 'Must send 10 ether'
}
}, 'Error event when sending 5 ether');
完全披露:我是这个包的作者。我在 SO 上寻找这样的解决方案后写了它,但找不到。
您可以使用 truffle-assertions
包,它有一个断言来检查事件是否已发出。它还可以选择传递过滤器函数,以便检查事件参数的复杂条件。
npm install truffle-assertions
您可以在测试文件的顶部导入它:
const truffleAssert = require('truffle-assertions');
并在你的测试中使用它:
let txResult = await insurance.send({from: accounts[0], value: web3.toWei(5, 'ether')});
truffleAssert.eventEmitted(txResult, 'Error', (ev) => {
return ev.error === 'Must send 10 ether';
}
免责声明:我创建这个包是为了在我自己的测试中使用,通过添加过滤器函数,可以以一种非常直接的方式检查事件参数的复杂条件。我在我的博客上写了an article explaining this in more detail。
除了自己编写,您还可以使用 Truffle 的测试工具 expectEvent.js:
const { inLogs } = require('openzeppelin-solidity/test/helpers/expectEvent')
require('chai').use(require('chai-bignumber')(BigNumber)).should()
...
{ logs: this.logs } = await this.token.burn(amount, { from: owner })
...
const event = inLogs(this.logs, 'Burn')
event.args.burner.should.eq(owner)
event.args.value.should.be.bignumber.equal(amount)
可以在 Truffle 的 BurnableToken.behavior.js 中找到示例。
我能够找到一些参考来帮助解决这个问题,特别是如果你想使用 async/await。
it('can create a unique star and get its name', async function () {
const event = this.contract.starClaimed({});
event.watch(async (err, result) => {
if (err) console.log("Somethings wrong...");
if (result.args.owner === accounts[0]) {
let token = result.args._token;
event.stopWatching;
const star = await this.contract.tokenIdToStarInfo(token);
assert.equal(star[0], 'awesome star!');
assert.equal(star[1], 'yada yada yada');
assert.equal(star[2], 'dec_test');
assert.equal(star[3], 'mag_test');
assert.equal(star[4], 'cent_test');
}
});
await this.contract.createStar('awesome star!', 'yada yada yada', 'dec_test', 'mag_test', 'cent_test', {from: accounts[0]});
});
这是我找到的参考资料:https://github.com/trufflesuite/truffle-contract/issues/117
我有一个合约函数,它会在每次调用时发出事件。
我想在每个通过的测试上发出一个事件,这里有一些测试:
it("should emit Error event when sending 5 ether", function(done){
var insurance = CarInsurance.deployed();
insurance.send({from: accounts[0], value: web3.toWei(5, 'ether')}).then(done).catch(done);
});
it("should emit Error event when sending 5 ether", function(done){
var insurance = CarInsurance.deployed();
insurance.send({from: accounts[0], value: web3.toWei(5, 'ether')}).then(function(txHash){
assert.notEqual(txHash, null);
}).then(done).catch(done);
});
it("should emit Error event when sending 5 ether", function(done){
var insurance = CarInsurance.deployed();
insurance.send({from: accounts[0], value: web3.toWei(5, 'ether')}).then(function(done){
done();
}).catch(done);
});
结果是:
1) should emit Error event when sending 5 ether
Events emitted during test:
---------------------------
Error(error: Must send 10 ether)
---------------------------
✓ should emit Error event when sending 5 ether (11120ms)
✓ should emit Error event when sending 5 ether (16077ms)
3 passing (51s)
1 failing
1) Contract: CarInsurance should emit Error event when sending 5 ether:
Error: done() invoked with non-Error: 0x87ae32b8d9f8f09dbb5d7b36267370f19d2bda90d3cf7608629cd5ec17658e9b
可以看到只有一个记录失败了。
有什么想法吗?
谢谢
您正在将交易哈希传递给 done() 函数。我认为问题在于:
insurance.send({from: accounts[0], value: web3.toWei(5, 'ether')}).then(done).catch(done);
改为:
insurance.send({from: accounts[0], value: web3.toWei(5, 'ether')}).then(function() { done(); }).catch(done);
测试事件:
it("should check events", function(done) {
var watcher = contract.Reward();
// we'll send rewards
contract.sendReward(1, 10000, {from: accounts[0]}).then(function() {
return watcher.get();
}).then(function(events) {
// now we'll check that the events are correct
assert.equal(events.length, 1);
assert.equal(events[0].args.beneficiary.valueOf(), 1);
assert.equal(events[0].args.value.valueOf(), 10000);
}).then(done).catch(done);
});
从 Truffle v3 开始,您可以在回调结果中获取日志。所以你可以这样做:
insurance.send({from: accounts[0], value: web3.toWei(5, 'ether')}).then((result) => {
assert.equal(result.logs[0].event, "Error", "Expected Error event")
})
见https://github.com/trufflesuite/truffle-contract#processing-transaction-results
有一个帮手可以做到这一点:
npm install --save truffle-test-utils
在测试的顶部:
require('truffle-test-utils').init();
在你的测试中:
let result = await insurance.send({from: accounts[0], value: web3.toWei(5, 'ether')});
assert.web3Event(result, {
event: 'Error',
args: {
error: 'Must send 10 ether'
}
}, 'Error event when sending 5 ether');
完全披露:我是这个包的作者。我在 SO 上寻找这样的解决方案后写了它,但找不到。
您可以使用 truffle-assertions
包,它有一个断言来检查事件是否已发出。它还可以选择传递过滤器函数,以便检查事件参数的复杂条件。
npm install truffle-assertions
您可以在测试文件的顶部导入它:
const truffleAssert = require('truffle-assertions');
并在你的测试中使用它:
let txResult = await insurance.send({from: accounts[0], value: web3.toWei(5, 'ether')});
truffleAssert.eventEmitted(txResult, 'Error', (ev) => {
return ev.error === 'Must send 10 ether';
}
免责声明:我创建这个包是为了在我自己的测试中使用,通过添加过滤器函数,可以以一种非常直接的方式检查事件参数的复杂条件。我在我的博客上写了an article explaining this in more detail。
除了自己编写,您还可以使用 Truffle 的测试工具 expectEvent.js:
const { inLogs } = require('openzeppelin-solidity/test/helpers/expectEvent')
require('chai').use(require('chai-bignumber')(BigNumber)).should()
...
{ logs: this.logs } = await this.token.burn(amount, { from: owner })
...
const event = inLogs(this.logs, 'Burn')
event.args.burner.should.eq(owner)
event.args.value.should.be.bignumber.equal(amount)
可以在 Truffle 的 BurnableToken.behavior.js 中找到示例。
我能够找到一些参考来帮助解决这个问题,特别是如果你想使用 async/await。
it('can create a unique star and get its name', async function () {
const event = this.contract.starClaimed({});
event.watch(async (err, result) => {
if (err) console.log("Somethings wrong...");
if (result.args.owner === accounts[0]) {
let token = result.args._token;
event.stopWatching;
const star = await this.contract.tokenIdToStarInfo(token);
assert.equal(star[0], 'awesome star!');
assert.equal(star[1], 'yada yada yada');
assert.equal(star[2], 'dec_test');
assert.equal(star[3], 'mag_test');
assert.equal(star[4], 'cent_test');
}
});
await this.contract.createStar('awesome star!', 'yada yada yada', 'dec_test', 'mag_test', 'cent_test', {from: accounts[0]});
});
这是我找到的参考资料:https://github.com/trufflesuite/truffle-contract/issues/117