Lesson 14, Random IPFS NFT Test #257
-
Trying to build some tests on my own I got to this for describe("fulfillrandomWords", function () {
it("should increase token counter, set URI, emit event", async function () {
const tokenCounterBefore = await randomIpfsNft.getTokenCounter()
const txResponse = await randomIpfsNft.requestNft({ value: FUND_AMOUNT })
const txReceipt = await txResponse.wait(1)
const requestId = txReceipt.events[1].args.requestId
await expect(
vrfCoordinatorV2Mock.fulfillRandomWords(requestId, randomIpfsNft.address)
).to.emit(randomIpfsNft, "NftMinted")
const tokenCounterAfter = await randomIpfsNft.getTokenCounter()
const tokenUri = await randomIpfsNft.getDogTokenUris(0)
assert.equal(
(tokenCounterBefore.toNumber() + 1).toString(),
tokenCounterAfter.toString()
)
assert(tokenUri.toString() !== "")
})
}) However, checking on the tests here in the repo later on, I noticed that a describe("fulfillRandomWords", () => {
it("mints NFT after random number returned", async function () {
await new Promise(async (resolve, reject) => {
randomIpfsNft.once("NftMinted", async () => {
try {
const tokenUri = await randomIpfsNft.tokenURI(0)
const tokenCounter = await randomIpfsNft.getTokenCounter()
assert.equal(tokenUri.toString().includes("ipfs://"), true)
assert.equal(tokenCounter.toString(), "1")
resolve()
} catch (e) {
console.log(e)
reject(e)
}
})
try {
const fee = await randomIpfsNft.getMintFee()
const requestNftResponse = await randomIpfsNft.requestNft({
value: "10000000000000000",
})
const requestNftReceipt = await requestNftResponse.wait(1)
await vrfCoordinatorV2Mock.fulfillRandomWords(
requestNftReceipt.events[1].args.requestId,
randomIpfsNft.address
)
} catch (e) {
console.log(e)
reject(e)
}
})
})
}) Both tests pass, with the only difference being that the version with the Now my question: is there any significant difference between the effectiveness of the two? Could the version with no |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
There is a difference :) -The promise-based one is a more realistic version
RationaleIn your test, when you run: await expect(
vrfCoordinatorV2Mock.fulfillRandomWords(requestId, randomIpfsNft.address)
).to.emit(randomIpfsNft, "NftMinted") To mock the chainlink node response. But on a real network, we'd have to Does that make sense? This is another way of saying that, if you tried your version on a testnet, it would fail! |
Beta Was this translation helpful? Give feedback.
There is a difference :)
-The promise-based one is a more realistic version
Rationale
In your test, when you run:
const tokenUri = await randomIpfsNft.getDogTokenUris(0)
, this assumes that the tokenURI of the dog has already been created. On a local net, we ran 👍To mock the chainlink node response. But on a real network, we'd have to
await
for the Chainlink node to respond. So we setup a listener inside a promise to listen for the response before we make the call to m…