1

I'm trying to use OZ Test helpers to pass a test when a specific event is emitted. Here is the logic:

  • I call function1 with a JS Promise.
  • function1 calls function2 which emits an event.

Parts of code have been omitted for readability:

test.js

const { expectEvent } = require('@openzeppelin/test-helpers');
const { ethers }      = require("hardhat");

describe("test", function() => {
let mycontract;

before( async() => {
    const Mycontract = await ethers.getContractFactory("MyContract");
    mycontract = await Mycontract.deploy();
})

it("expected event is emitted.", async() => {

    const{ hash } = await mycontract.function1();

    await expectEvent.inTransaction(hash, mycontract, "THIS_EVENT",
     {message:"This event has happened."} );
 });
});

myContract.sol

pragma solidity ^0.6.0;

contract MyContract
{
string message;

constructor(){
 _message = "This event has happened."
}

event THIS_EVENT(string message);

function1() {
 function2();
}

function2() {
 emit THIS_EVENT("_message")
 }

}

Error message:

     Error: Unknown contract object

How can I write a test to pass when a specific event is emitted as a result of a nested function call ?

Your time is much appreciated,
Edit hardhat: 2.2.1 test-helpers: 0.1.4

2
  • Okay can u please add the versions of the libraries
    – Majd TL
    Commented May 5, 2021 at 19:45
  • @MajdTL just added
    – newbnoob
    Commented May 9, 2021 at 8:53

1 Answer 1

0

Try this

it('expected event is emitted', async function() {
    const receipt = await this.mycontract.function1();
    expectEvent(receipt, 'THIS_EVENT', {
        message: 'This event has happened.',
    });
});
2
  • Your code returns: TypeError: Cannot read property 'getRep' of undefined However when I replace this.mycontract with mycontract I get : ` Error: Unknown transaction receipt object `
    – newbnoob
    Commented May 5, 2021 at 15:02
  • Okay sorry I tested that on truffle and not hardhat
    – Majd TL
    Commented May 5, 2021 at 19:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.