gpt4 book ai didi

events - 使用 truffle 测试以太坊事件日志

转载 作者:行者123 更新时间:2023-12-02 02:06:16 27 4
gpt4 key购买 nike

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

我希望在每个通过的测试上发出一个事件,这里有一些测试:

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com