Lesson 14: Unit-testing 'emit Minted()' not working, also txReceipt-issue #2287
-
Hi all, 2 questions: 1). I'm trying to Unit test the Seems I'm missing something that may have been in place while testing events in BasicNft.sol // SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
contract BasicNft is ERC721 {
event Minted(address indexed owner, uint256 indexed tokenId);
uint256 private s_tokenCounter;
string public constant TOKEN_URI = "ipfs://bafybeig37ioir76s7mg5oobetncojcm3c3hxasyd4rvid4jqhy4gkaheg4/?filename=0-PUG.json";
constructor () ERC721 ("Doggie", "DOG") {
s_tokenCounter = 0;
}
function mintNft() public {
_safeMint(msg.sender, s_tokenCounter);
emit Minted(msg.sender, s_tokenCounter);
s_tokenCounter++;
// return s_tokenCounter; // not serving any purpose, can be removed
}
function tokenURI(uint256 tokenId) public view override returns (string memory) {
return TOKEN_URI;
}
function getTokenCounter() public view returns(uint256) {
return s_tokenCounter;
}
} ==================== BasicNft-test.js const { assert, expect } = require("chai")
const { deployments, getNamedAccounts, ethers, network } = require("hardhat")
const { developmentChains, networkConfig } = require("../../helper-hardhat-config")
!developmentChains.includes(network.name) ? describe.skip : describe("BasicNft Unit Tests", function () {
let basicNft, deployer, tokenCounter
beforeEach(async function () {
console.log("BasicNft.sol deploying...")
deployer = (await getNamedAccounts()).deployer
await deployments.fixture(["all"])
basicNft = await ethers.getContract("BasicNft", deployer)
console.log("BasicNft.sol deployed!!")
})
describe("Testing Constructor", function () {
it ("Should assign correct values to 'Name', 'Symbol', and 'TokenId'", async function () {
const name = await basicNft.name()
const symbol = await basicNft.symbol()
tokenCounter = await basicNft.getTokenCounter()
assert.equal(name, "Doggie")
assert.equal(symbol, "DOG")
assert.equal(tokenCounter.toString(), "0")
})
})
describe("Testing MintNft()", function() {
it("Should emit Minted event", async function() {
tokenCounter = await basicNft.getTokenCounter()
console.log(`Deployer: ${deployer}`)
console.log(`Token Counter: ${tokenCounter}`)
await expect(await basicNft.mintNft().to.emit(basicNft, "Minted").withArgs(deployer, tokenCounter))
})
it("Updated 2 mappings: _balances[] and _owners[] & incremented tokenCounter", async function() {
tokenCounter = await basicNft.getTokenCounter()
const txResponse = await basicNft.mintNft()
const txReceipt = await txResponse.wait(1)
console.log(`Transaction Receipt: ${txReceipt}`)
const tokenBalance = await basicNft.balanceOf(deployer)
const tokenOwner = await basicNft.ownerOf(tokenCounter)
// checking 2 private mappings
assert.equal(tokenBalance.toString(), "1")
assert.equal(tokenOwner.toString(), deployer)
// checking whether tokenCounter incremented by 1
tokenCounter = await basicNft.getTokenCounter()
assert.equal(tokenCounter.toString(), "1")
})
})
}) It is outputting the correct values of Not sure what is ============================ 2). I'm also trying to print P.S: I know we do not need to perform second Not sure the issue here either: |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 10 replies
-
This line await expect(await basicNft.mintNft().to.emit(basicNft, "Minted").withArgs(deployer, tokenCounter)) should be await expect(await basicNft.mintNft().to.emit("Minted").withArgs(deployer, tokenCounter)) |
Beta Was this translation helpful? Give feedback.
-
@ManuWeb3 : There are couple of things I wanna correct :
And, yes
Because you should always check the token counter after minting an NFT for it to update, because you were checking
Your updated above
And Your Promise is resolved, It is just not being console.logged properly : Change your To : |
Beta Was this translation helpful? Give feedback.
@ManuWeb3 : There are couple of things I wanna correct :
In your
mintNft
function in yourBasicNft
contract, You were updating the token counter after minting the NFT, which is incorrect.Always update your token counter before minting the NFT is the best practice and preferable way, and also will not create any problems for you later on!
It should be like this :
And, yes
return s_tokenCounter;
is not needed.basicNft.t…