测试以太坊事件日志与松露

lth*_*ron 11 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);
});
Run Code Online (Sandbox Code Playgroud)

结果是:

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
Run Code Online (Sandbox Code Playgroud)

您可以看到记录的唯一一个失败.

任何的想法 ?

谢谢

Ald*_*ein 12

您正在将tx哈希传递给done()函数.我认为问题在于:

insurance.send({from: accounts[0], value: web3.toWei(5, 'ether')}).then(done).catch(done);
Run Code Online (Sandbox Code Playgroud)

将其更改为:

insurance.send({from: accounts[0], value: web3.toWei(5, 'ether')}).then(function() { done(); }).catch(done);
Run Code Online (Sandbox Code Playgroud)

测试事件:

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);
});
Run Code Online (Sandbox Code Playgroud)


小智 6

从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


phi*_*e_b 5

有一个帮手可以做到这一点:

npm install --save truffle-test-utils
Run Code Online (Sandbox Code Playgroud)

在测试的顶部:

require('truffle-test-utils').init();
Run Code Online (Sandbox Code Playgroud)

在你的测试中:

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');
Run Code Online (Sandbox Code Playgroud)

完全披露:我是这个包的作者.我在SO上寻找这样的解决方案后写了它,但找不到它.