Events 使用块菌测试以太坊事件日志

Events 使用块菌测试以太坊事件日志,events,logging,mocha.js,ethereum,truffle,Events,Logging,Mocha.js,Ethereum,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(don

我有一个契约函数,它在每次调用时发出事件

我希望在通过的每个测试中都发出一个事件,以下是一些测试:

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
您可以看到,唯一记录的一个失败

有什么想法吗


谢谢

您正在将tx散列传递到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);
});

自TruffleV3以来,您将在回调结果中获得日志。所以你可以这样做:

insurance.send({from:accounts[0],value:web3.toWei(5,'ether')})。然后((result)=>{
assert.equal(result.logs[0]。事件,“错误”,“预期的错误事件”)
})


请参见

有一个帮助程序可以完成此操作:

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');
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';
}
在您的测试中:

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');
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';
}

充分披露:我是这个软件包的作者。我是在寻找这样的解决方案后写的,但是找不到

您可以使用
truffle断言
包,它有一个断言来检查事件是否已发出。它还可以选择传递过滤器函数,以便检查事件参数的复杂条件

npm install truffle-assertions
您可以在测试文件的顶部导入它:

const truffleAssert = require('truffle-assertions');
并在测试中使用它:

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');
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';
}

免责声明:我创建了这个包用于我自己的测试,通过添加filter函数,可以以非常简单的方式检查事件参数的复杂条件。我在我的博客上写过。

您也可以使用Truffle的test utils,而不是自己写:


可以在Truffle中找到一个例子。

我可以找到一些参考资料来帮助您,特别是如果您想使用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]});
    });

下面是我找到的参考:

事实上,只有第一个测试输出我想要的“错误”事件。当我像你们说的那个样更改时,它并没有输出任何内容,似乎它在等待完成()…@user3262670我在测试中根本并没有看到任何事件检查所有测试用例都被命名为“发送5以太时应发出错误事件”,所以您无法判断其中哪些失败。我们可以在测试中检查事件吗?我想展示做相同测试用例的三种方法。我只想在测试期间显示事件,唯一的方法是在测试失败时…@user3262670我添加了一个如何在Truffle中测试事件的示例。Truffle是否直接从solidity支持测试事件?