我在youtube上学习本教程"https://www.youtube.com/watch?v=7QsqElEaWBQ">
我已经仔细检查了一遍,以确保我的代码匹配,但我被困在测试项目的41:00分钟标记处。它一贯地显示";0通过";。没有我在教程视频中看到的失败消息。这让我相信他们甚至没有接受测试。我已经安装了一开始所需的所有依赖项,检查以确保我的文件名匹配,但仍然没有成功。此测试使用的是安全帽华夫饼干。
这是我的";测试.js";文件代码=>
const { expect } = require("chai");
const { ethers } = require("ethers");
const {
isCallTrace,
} = require("hardhat/internal/hardhat-network/stack-traces/message-trace");
describe("Staking", function () {
beforeEach(async function () {
[signer1, signers2] = await ethers.getSigners();
Staking = await ethers.getContractFactory("Staking", signer1);
staking = await Staking.deploy({
value: ethers.utils.parseEther("10"),
});
});
describe("deploy", function () {
it("should set owner", async function () {
expect(await staking.owner()).to.equal(signer1.address);
});
it("sets up tiers and lockPeriods", async function () {
expect(await staking.lockPeriods(0)).to.equal(30);
expect(await staking.lockPeriods(1)).to.equal(90);
expect(await staking.lockPeriods(2)).to.equal(180);
expect(await staking.tiers(3)).to.equal(700);
expect(await staking.tiers(3)).to.equal(1000);
expect(await staking.tiers(3)).to.equal(1200);
});
});
describe("stakeEther", function () {
it("transfers ether", async function () {
const provider = waffle.provider;
let contractBalance;
let signerBalance;
const transferAmount = ethers.utils.parseEther("2.0");
contractBalance = await provider.getBalance(staking.address);
signerBalance = await signer1.getBalance();
const data = { value: transferAmount };
const transaction = await staking.connect(signer1).stakeEther(30, data);
const receipt = await transaction.wait();
const gasUsed = receipt.gasUsed.mul(receipt.effectiveGasPrice);
//test the change in signer1's ether balance
expect(await signer1.getBalance()).to.equal(
signerBalance.sub(transferAmount).sub(gasUsed)
);
// test the change in contract's ether balance
expect(await provider.getBalance(staking.address)).to.equal(
contractBalance.add(transferAmount)
);
});
});
});
如果你知道如何解决我的问题,请告诉我。那对我将是一个很大的帮助!
请确保Testing.js
位于文件夹test
中,文件夹test
位于package.json 旁边
另一个可能导致这种情况的问题是,如果您在项目中初始化了git,然后创建了一个新文件夹,在本例中为test
,该文件夹将具有untracked flag - U
。通过donggit add .
添加未跟踪的文件或文件夹并提交git commit -m "whatever message you want"
,然后使用npm
或yarn
运行测试命令。这对我很有效。希望能有所帮助。干杯