繁体   English   中英

before, beforeEach 不在安全帽测试中执行

[英]before, beforeEach does not execute in hardhat test

此代码用于测试用安全帽编写的 Silent Auction 智能合约。

当我在 it{...} 块中分别输入所有内容时,测试通过了。 当使用 before 和 beforeEach 来简化代码时,它不起作用。

错误消息:“ReferenceError:silentAuction 未定义”

有人可以告诉我哪一部分我做错了吗?

const { BigNumber } = require("ethers");
const { ethers } = require("hardhat");

describe("SilentAuction", function () {

  before(async function () {
    const SilentAuction = await ethers.getContractFactory("SilentAuction");
  })

  beforeEach(async function () {
    const silentAuction = await SilentAuction.deploy();
    await silentAuction.deployed();
  })

  describe("- setItem function tests", function (){
    it("Owner should be able to set an item for bid", async function () {
      //const SilentAuction = await ethers.getContractFactory("SilentAuction");
      //const silentAuction = await SilentAuction.deploy();
      //await silentAuction.deployed();
      await silentAuction.setItem("SAMPLE", 100, 300);

      expect((await silentAuction.items(0)).name).to.be.equal("SAMPLE");
      expect((await silentAuction.items(0)).highestPrice.toString()).to.be.equal('100');
      expect((await silentAuction.items(0)).successLimit.toString()).to.be.equal('300');
    });

    it("Not owner cannot call function", async function () {
      const [owner, addr1] = await ethers.getSigners();
      //const SilentAuction = await ethers.getContractFactory("SilentAuction");
      //const silentAuction = await SilentAuction.deploy();
      //await silentAuction.deployed();

      await expect(silentAuction.connect(addr1).setItem("SAMPLE", 100, 300)).to.be.reverted;
    });
  });

  describe("- bid function tests", function (){
    it("Everyone should place a bid", async function () {
      const [owner, addr1] = await ethers.getSigners();
      //const SilentAuction = await ethers.getContractFactory("SilentAuction");
      //const silentAuction = await SilentAuction.deploy();
      //await silentAuction.deployed();

      await silentAuction.connect(owner).setItem("SAMPLE", 100, 300);
      await silentAuction.connect(addr1).bid(200);
      expect((await silentAuction.items(0)).highestPrice.toString()).to.be.equal('200');
    })

    it("Should not replace smaller bids than the recent highest", async function () {
      const [owner, addr1] = await ethers.getSigners();
      //const SilentAuction = await ethers.getContractFactory("SilentAuction");
      //const silentAuction = await SilentAuction.deploy();
      //await silentAuction.deployed();

      await silentAuction.connect(owner).setItem("SAMPLE", 100, 300);
      await silentAuction.connect(addr1).bid(50);
      expect((await silentAuction.items(0)).highestPrice.toString()).to.be.equal('100');
    })
  })
});

silentAuction是一个常量,在 before 或 beforeEach 函数的范围内定义,而不是在整个测试的范围内。

如果你想在整个测试中使用这样的变量,你应该这样做:

describe('foo' () => {
  let silentAuction;

  beforeEach(() => {
     silentAuction = ...
  })
})

另外你得到引用错误的原因是每个测试函数都无法在其或外部范围内找到这样的变量,然后它会引发引用错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM