用松露的测试迷信事件日志 [英] Test ethereum Event Logs with truffle

查看:167
本文介绍了用松露的测试迷信事件日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想在每次测试中发送一个事件,这些都是一些测试:

 它(发送发送5以太时会发出错误事件,function(done){
var insurance = CarInsurance .deployed();

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

它(发送错误事件发送5以太时,函数(完成){
var insurance = CarInsurance.deployed();

insurance.send ({from:accounts [0],value:web3.toWei(5,'ether')})然后(function(txHash){
assert.notEqual(txHash,null);
}) .then(done).catch(done);
});

它(发送错误事件发送5以太时,函数(完成){
var insurance = CarInsurance.deployed();

insurance.send ({from:accounts [0],value:web3.toWei(5,'ether')})然后(function(done){
done();
})catch(done) ;
});

结果是:

  1)发送错误事件5以太

测试期间发出的事件:
---------------- -----------

错误(错误:必须发送10个以太)

-------------- -------------
✓发送5个以太网(11120ms)时应发出错误事件
✓发送错误事件时发送5个以太网(16077ms)


3通过(51s)
1失败

1)合同:CarInsurance应发送错误事件发送5 ether:
错误:done()被调用与非错误: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 ); 

要测试活动:

 它(应该检查事件,function(done){
var watcher = contract.Reward();

//我们将发送奖励
contract.sendReward(1,10000,{from:accounts [0]})然后(function(){
return watcher.get();
})然后(function ){
//现在我们将检查事件是否正确
assert.equal(events.length,1);
assert.equal(events [0] .args.beneficiary。 valueOf(),1);
assert.equal(events [0] .args.value.valueOf(),10000);
})然后(完成).catch(done);
});


I have a contract's function which emit events on each call.

I would like to have an event emitted on each test which are passing, here are some tests :

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

The results are :

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

You can see that the only one which is logged fail.

Any idea ?

Thank you

解决方案

You are passing tx hash into done() function. I think the problem is in line:

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

Change it to:

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

To test for events:

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

这篇关于用松露的测试迷信事件日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆