events - 使用 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(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 哈希值传递给 did() 函数。我认为问题出在:

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

关于events - 使用 truffle 测试以太坊事件日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36627733/

相关文章:

javascript - Polymer:回调VS事件

javascript - 输入状态在第一次单击时未绑定(bind) onChange

python - 使用 Python 将多个函数写入单个日志文件

logging - TraceSource 的 Common.Logging

wpf - ListView 上的 TextBlock : how to ignore clicks in the TextBlock and let the listview handle them

javascript - jquery 等待多个完成事件

c# - 简单的 C# 日志文件

javascript - Mocha : Is there a scenario where a before hook would run after a test?

mocha.js - 发送参数以使用 Istanbul 尔测试脚本

knockout.js - 使用 mocha 进行 headless knockout View 模型测试