rkalis / truffle-assertions

🛠 Assertions and utilities for testing Ethereum smart contracts with Truffle unit tests
https://kalis.me/check-events-solidity-smart-contract-test-truffle/
MIT License
154 stars 20 forks source link

AssertionError when an event is emitted #3

Closed hdahme closed 6 years ago

hdahme commented 6 years ago

Hi!

Firstly, thanks for this incredibly useful library. It makes my whole testing flow that much more manageable.

I'm running into an issue where the an AssertionError: Event filter for <eventName> returned no results is thrown, when, whilst running the tests, the output is

Events emitted during test:
    ---------------------------
    eventName([args])
    ---------------------------

Is this a bug?

rkalis commented 6 years ago

Hi @hdahme, thanks for opening this issue.

Could you share the event signature and the assertion code? If you're not comfortable sharing it here, could you send it to my email (roscokalis@gmail.com), or could you create a small test with a demo event that is very similar to your actual use case?

hdahme commented 6 years ago

Hey @rkalis , thanks for your speedy reply!

Here's an example contract (AssertionTest.sol)

pragma solidity ^0.4.23;

import "openzeppelin-solidity/contracts/token/ERC20/StandardToken.sol";

contract AssertionTest is StandardToken {

    constructor() public {
    }

    event TestEvent(uint8 i);

    function emitTestEvent() public returns (bool) {
      emit TestEvent(1);
      return true;
    }
}

and the test (TestAssertionTest.js)

var AssertionTest = artifacts.require("AssertionTest");

const assert = require("chai").assert;
const truffleAssert = require('truffle-assertions');

contract('AssertionTest', function(accounts) {
  let token;
  const owner = accounts[0];

  beforeEach(async function() {
    token = await AssertionTest.new({ from: owner });
  });

  it("should emit an event", async function() {
    let tx = await token.emitTestEvent();

    truffleAssert.eventEmitted(tx, 'TestEvent', (ev) => {
        return ev.i === 1;
    });
  });

});

and the output from truffle test;

  Contract: AssertionTest
    1) should emit an event

    Events emitted during test:
    ---------------------------

    TestEvent(i: 1)

    ---------------------------
  1) Contract: AssertionTest
       should emit an event:
     AssertionError: Event filter for TestEvent returned no results
      at assertEventListNotEmpty (node_modules/truffle-assertions/index.js:19:11)
      at Object.eventEmitted (node_modules/truffle-assertions/index.js:48:5)
      at Context._callee2$ (test/TestAssertionTest.js:17:19)
      at tryCatch (node_modules/regenerator-runtime/runtime.js:65:40)
      at Generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:303:22)
      at Generator.prototype.(anonymous function) [as next] (node_modules/regenerator-runtime/runtime.js:117:21)
      at step (test/TestAssertionTest.js:3:191)
      at test/TestAssertionTest.js:3:361
      at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:188:7)

Feel free to use this as an example =)

hdahme commented 6 years ago

Ohhh... got it.

If I use

truffleAssert.eventEmitted(tx, 'TestEvent', (ev) => {
        return ev.i == 1;
    });

instead of

truffleAssert.eventEmitted(tx, 'TestEvent', (ev) => {
        return ev.i === 1;
    });

it passes.

Thanks for this awesome library =)

rkalis commented 6 years ago

Hi @hdahme, good to hear that the issue was resolved 🙂. Good luck with your further Solidity development!

ghost commented 4 years ago

you can do return ev.i.toNumber() === 1; and it'll work