12

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

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

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

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

任何想法 ?

谢谢

4

6 回答 6

12

您正在将 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);
});
于 2016-04-15T02:32:49.097 回答
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

于 2017-02-10T08:26:18.890 回答
5

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

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

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

于 2018-02-19T10:03:19.730 回答
2

除了自己编写,您还可以使用 Truffle 的测试工具 expectEvent.js

const { inLogs } = require('openzeppelin-solidity/test/helpers/expectEvent')
require('chai').use(require('chai-bignumber')(BigNumber)).should()
...
{ logs: this.logs } = await this.token.burn(amount, { from: owner })
...
const event = inLogs(this.logs, 'Burn')
event.args.burner.should.eq(owner)
event.args.value.should.be.bignumber.equal(amount)

可以在 Truffle 的BurnableToken.behavior.js中找到一个示例。

于 2018-08-17T05:29:49.093 回答
0

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

npm install truffle-assertions

您可以在测试文件的顶部导入它:

const truffleAssert = require('truffle-assertions');

并在您的测试中使用它:

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

免责声明:我创建了这个包以在我自己的测试中使用,并且通过添加过滤器功能,可以以非常直接的方式检查事件参数的复杂条件。我在我的博客上写了一篇文章更详细地解释了这一点。

于 2018-04-22T17:45:12.250 回答
0

我能够找到一些参考资料来帮助解决这个问题,特别是如果你想使用 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]});
    });

这是我找到的参考资料:https ://github.com/trufflesuite/truffle-contract/issues/117

于 2018-11-15T17:19:08.553 回答