const{ ethers }=require("hardhat")const{ assert, expect }=require("chai")// Mocha 测试框架会识别这个describe函数describe("SimpleStorage",function(){let simpleStorageFactory, simpleStorage
beforeEach(asyncfunction(){
simpleStorageFactory =await ethers.getContractFactory("SimpleStorage")
simpleStorage =await simpleStorageFactory.deploy()})// 测试做什么,用函数来实际执行这个操作it("Should start with a favorite number of 0",asyncfunction(){const currentValue =await simpleStorage.retrieve()const expectedValue ="0"
assert.equal(currentValue.toString(), expectedValue)// expect(currentValue.toString()).to.equal(expectedValue) // 等价于上面})it("Should update when we call store",asyncfunction(){const expectedValue ="7"const transactionResponse =await simpleStorage.store(expectedValue)await transactionResponse.wait(1)const currentValue =await simpleStorage.retrieve()
assert.equal(currentValue.toString(), expectedValue)})})
运行命令yarn hardhat test
>yarn hardhat testyarn run v1.22.19
..\node_modules\.bin\hardhat test
SimpleStorage
✔ Should start with a favorite number of 0
✔ Should update when we call store
2 passing (2s)
Done in2.28s.
>yarn hardhat test--grep store
yarn run v1.22.19
$ E:\web3_solidity\hardhat-solidity01\node_modules\.bin\hardhat test--grep store
SimpleStorage
✔ Should update when we call store
1 passing (2s)
Done in2.26s.
运行指定某一个it测试,【方式二】:用it.only来代替it,然后运行命令yarn hardhat test
//test-deploy.js
it.only("Should start with a favorite number of 0",asyncfunction(){const currentValue =await simpleStorage.retrieve()const expectedValue ="0"
assert.equal(currentValue.toString(), expectedValue)})
>yarn hardhat testyarn run v1.22.19
..\node_modules\.bin\hardhat test
SimpleStorage
✔ Should start with a favorite number of 01 passing (2s)
Done in2.24s.