I have a Solidity contract that inherits from ERC721PresetMinterPauserAutoId
I want to write a test that verifies that a Transfer event has been emitted by the contract after the public mint(to) function has been invoked. Here's what my tests look like so far:
const { expect } = require("chai");
const { ethers } = require("hardhat");
describe("MyContract -- Contract Tests", function () {
let admin, addr1, addr2;
let myContract;
let minterRole = ethers.utils.id("MINTER_ROLE");
beforeEach(async () => {
([admin, addr1, addr2] = await ethers.getSigners());
const MyContract = await ethers.getContractFactory('MyContract');
myContract = await MyContract.deploy();
});
// Test passes
it("Should not allow account without MINTER_ROLE to mint", async function() {
await expect(myContract.connect(addr1).mint(addr1.address))
.to.be.revertedWith('ERC721PresetMinterPauserAutoId: must have minter role to mint');
});
it("Should allow account with MINTER_ROLE to mint", async function() {
await myContract.connect(admin).grantRole(minterRole, addr1.address);
expect(await myContract.hasRole(minterRole, addr1.address)).to.equal(true);
await expect(myContract.connect(addr1).mint(addr1.address))
.to.emit(myContract, 'Transfer')
.withArgs(address(0), addr1.address, 1); // <-- this part is broken
});
});
Obviously address(0) is undefined. I looked at the source code for the underlying _mint function and it looks like the following:
function _mint(address to, uint256 tokenId) internal virtual {
require(to != address(0), "ERC721: mint to the zero address");
require(!_exists(tokenId), "ERC721: token already minted");
_beforeTokenTransfer(address(0), to, tokenId);
_balances[to] += 1;
_owners[tokenId] = to;
emit Transfer(address(0), to, tokenId);
}
So I have a few questions.
- How do I capture the arguments captured for the emitted event in my test so I can debug the output?
- What is the correct way to test that the arguments emitted by the event? I'm hard coding the token counter to
1but that's because I haven't found an API yet that tells me the total number of tokens minted. Also, I'm not entirely sure how to test foraddress(0).
scripts/getEventsFromTx.jsfile in the linked example – Franco Victorio Sep 17 '21 at 12:01