使用@nomiclabs/hardhat-waffle 实现固定装置
Implementing fixtures with @nomiclabs/hardhat-waffle
在官方 waffle 文档中,您可能会找到下一个实现固定装置的方法:
import {expect} from 'chai';
import {loadFixture, deployContract} from 'ethereum-waffle';
import BasicTokenMock from './build/BasicTokenMock';
describe('Fixtures', () => {
async function fixture([wallet, other], provider) {
const token = await deployContract(wallet, BasicTokenMock, [
wallet.address, 1000
]);
return {token, wallet, other};
}
it('Assigns initial balance', async () => {
const {token, wallet} = await loadFixture(fixture);
expect(await token.balanceOf(wallet.address)).to.equal(1000);
});
it('Transfer adds amount to destination account', async () => {
const {token, other} = await loadFixture(fixture);
await token.transfer(other.address, 7);
expect(await token.balanceOf(other.address)).to.equal(7);
});
});
但是,在安全帽上使用插件时,这将不起作用。插件文档中没有给出官方说明。
在下面回答。
虽然您可以通过“Alt + 单击”每个变量自行找到解决方案,直到得出正确的类型结构,但最好使用此代码段:
以下适用于 Typescript,如果您想在 javascript 上使用它,只需切换到使用“require()”导入并摆脱类型:
import {Wallet, Contract} from "ethers";
import {MockProvider} from "ethereum-waffle";
import {ethers, waffle} from "hardhat";
const {loadFixture, deployContract} = waffle;
//Contract ABI
// For typescript only!
// In order to be able to import .json files make sure you tsconfig.json has set "compilerOptions" > "resolveJsonModule": true. My tsconfig.json at the bottom!
//For obvious reasons change this to the path of your compiled ABI
import * as TodoListABI from "../artifacts/contracts/TodoList.sol/TodoList.json";
//Fixtures
async function fixture(_wallets: Wallet[], _mockProvider: MockProvider) {
const signers = await ethers.getSigners();
let token: Contract = await deployContract(signers[0], TodoListABI);
return {token};
}
然后,在你的 mocha-chai 测试中
it("My unit test", async function () {
const {token} = await loadFixture(fixture);
// Your code....
});
我的 tsconfig.json 这个安全帽项目
{
"compilerOptions": {
"target": "es2018",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"outDir": "dist",
"resolveJsonModule": true
},
"include": ["./scripts", "./test"],
"files": ["./hardhat.config.ts"]
}
请记住,hardhat 使用 mocha 作为其测试 运行ner,因此您可以使用 mocha 在其文档中描述的钩子:before()、after()、beforeEach() 和 afterEach()。
下面是部署代币合约并使用合约实例进行 运行 测试的示例。
beforeEach(async function () {
Token = await ethers.getContractFactory("Token");
[owner, addr1, addr2, ...addrs] = await ethers.getSigners();
hardhatToken = await Token.deploy();
});
describe("Deployment", function () {
it("Should set the right owner", async function () {
expect(await hardhatToken.owner()).to.equal(owner.address);
});
});
在官方 waffle 文档中,您可能会找到下一个实现固定装置的方法:
import {expect} from 'chai';
import {loadFixture, deployContract} from 'ethereum-waffle';
import BasicTokenMock from './build/BasicTokenMock';
describe('Fixtures', () => {
async function fixture([wallet, other], provider) {
const token = await deployContract(wallet, BasicTokenMock, [
wallet.address, 1000
]);
return {token, wallet, other};
}
it('Assigns initial balance', async () => {
const {token, wallet} = await loadFixture(fixture);
expect(await token.balanceOf(wallet.address)).to.equal(1000);
});
it('Transfer adds amount to destination account', async () => {
const {token, other} = await loadFixture(fixture);
await token.transfer(other.address, 7);
expect(await token.balanceOf(other.address)).to.equal(7);
});
});
但是,在安全帽上使用插件时,这将不起作用。插件文档中没有给出官方说明。
在下面回答。
虽然您可以通过“Alt + 单击”每个变量自行找到解决方案,直到得出正确的类型结构,但最好使用此代码段:
以下适用于 Typescript,如果您想在 javascript 上使用它,只需切换到使用“require()”导入并摆脱类型:
import {Wallet, Contract} from "ethers";
import {MockProvider} from "ethereum-waffle";
import {ethers, waffle} from "hardhat";
const {loadFixture, deployContract} = waffle;
//Contract ABI
// For typescript only!
// In order to be able to import .json files make sure you tsconfig.json has set "compilerOptions" > "resolveJsonModule": true. My tsconfig.json at the bottom!
//For obvious reasons change this to the path of your compiled ABI
import * as TodoListABI from "../artifacts/contracts/TodoList.sol/TodoList.json";
//Fixtures
async function fixture(_wallets: Wallet[], _mockProvider: MockProvider) {
const signers = await ethers.getSigners();
let token: Contract = await deployContract(signers[0], TodoListABI);
return {token};
}
然后,在你的 mocha-chai 测试中
it("My unit test", async function () {
const {token} = await loadFixture(fixture);
// Your code....
});
我的 tsconfig.json 这个安全帽项目
{
"compilerOptions": {
"target": "es2018",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"outDir": "dist",
"resolveJsonModule": true
},
"include": ["./scripts", "./test"],
"files": ["./hardhat.config.ts"]
}
请记住,hardhat 使用 mocha 作为其测试 运行ner,因此您可以使用 mocha 在其文档中描述的钩子:before()、after()、beforeEach() 和 afterEach()。
下面是部署代币合约并使用合约实例进行 运行 测试的示例。
beforeEach(async function () {
Token = await ethers.getContractFactory("Token");
[owner, addr1, addr2, ...addrs] = await ethers.getSigners();
hardhatToken = await Token.deploy();
});
describe("Deployment", function () {
it("Should set the right owner", async function () {
expect(await hardhatToken.owner()).to.equal(owner.address);
});
});