Created
August 14, 2022 09:22
-
-
Save yheihei/505635e0e8a213ebfc6116f0be630527 to your computer and use it in GitHub Desktop.
hardhatを使ったテストコードのサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { expect } = require("chai"); | |
const { ethers } = require("hardhat"); | |
const { loadFixture } = require("@nomicfoundation/hardhat-network-helpers"); | |
describe("HollandGene contract", function () { | |
async function deployTokenFixture() { | |
// 1. fixture内でNFTのコントラクトを取得 | |
const HollandGene = await ethers.getContractFactory("HollandGene"); | |
// 2. 使用するウォレットを作成しておく(デフォルトでETHがめちゃくちゃ入っている) | |
const [owner, addr1, addr2] = await ethers.getSigners(); | |
// 3. コントラクトのコンストラクタに値を入れつつ、デプロイ | |
const hardhatToken = await HollandGene.deploy( | |
'HollandGene', | |
'HG', | |
'ipfs//metadataのjsonファイルをアップしたCIDを入れる/', | |
'invalid' | |
); | |
await hardhatToken.deployed(); | |
// 4. テスト内で使う変数を返却 | |
return { HollandGene, hardhatToken, owner, addr1, addr2 }; | |
} | |
it("mintしたらNFTがmint数分取得できていること", async function () { | |
// 1. fixtureを実行してテストデータを入れる | |
const { hardhatToken, owner, addr1 } = await loadFixture(deployTokenFixture); | |
// 2. addr1のウォレットをつなぎつつ、コントラクトのmint関数と叩く。valueでethを送っておかないとお金が足りないと怒られるので注意 | |
await hardhatToken.connect(addr1).mint(1, { value: ethers.utils.parseEther("1") }); | |
const tokenIds = await hardhatToken.walletOfOwner(addr1.address); | |
expect([ ethers.BigNumber.from("1") ]).to.deep.equal( | |
tokenIds | |
); | |
}); | |
it("ownerは0ethでmintできること", async function () { | |
const { hardhatToken, owner, addr1 } = await loadFixture(deployTokenFixture); | |
await hardhatToken.connect(owner).mint(1, { value: ethers.utils.parseEther("0") }); | |
const tokenIds = await hardhatToken.walletOfOwner(owner.address); | |
expect([ ethers.BigNumber.from("1") ]).to.deep.equal( | |
tokenIds | |
); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment