在truffle的单元测试中使用多个契约示例

uqxowvwt  于 2021-09-13  发布在  Java
关注(0)|答案(0)|浏览(200)

我正在为solidity契约进行单元测试,但在使用同一契约的不同示例时遇到了问题。我正在测试的功能如下:

bool ended;

function auctionEnd() public {
    require(now >= auctionEndTime, "Auction has not yet ended.");
    require(ended == false, "Auction has already been ended.");

    ended = true;
    emit AuctionEnded(highestBidder, highestBid);
}

我创建了两个不同的已部署契约示例来测试两个不同的场景( auctionEnd 功能在两种情况下表现相同,但合同中的其他功能将不同):

contract("Auction", (accounts) => {
  describe("One or more bidding", async() => {
    let auctionInstance, startingBidInput;

    beforeEach(async () => {
      startingBidInput = web3.utils.toWei("1", "ether");
      // the first parameter is the auction time limit
      auctionInstance = await Auction.deployed(300, startingBidInput);
    })

    it("End auction", async () => {
      // this progressed the time to be later than the auction end time
      const advancement = 600;
      await helper.advanceTimeAndBlock(advancement);

      let tx = await auctionInstance.auctionEnd({ from: accounts[0] });
      assert.isTrue(tx.receipt.status, "auctionEnded status is not true");
    })
  })

  describe("No bidding", async() => {
    let noBidAuctionInstance, startingBidInput2;

    beforeEach(async () => {
      startingBidInput2 = web3.utils.toWei("1", "ether");
      noBidAuctionInstance = await Auction.deployed(300, startingBidInput2);
    })

    it("End no bid auction", async () => {
      const advancement = 600;
      await helper.advanceTimeAndBlock(advancement);

      let tx = await noBidAuctionInstance.auctionEnd({ from: accounts[0] });
      assert.isTrue(tx.receipt.status, "auctionEnded status is not true");
    })
  })
})

当我执行测试时,我得到以下错误 let tx = await noBidAuctionInstance.auctionEnd({ from: accounts[0] }); :
错误:返回错误:处理事务时vm异常:还原拍卖已结束。-给出的理由:拍卖已经结束。。
我很困惑,因为我使用了两个不同的合同示例 auctionInstancenoBidAuctionInstanceauctionEnd 功能 noBidAuctionInstance 只调用了一次。是 ended 两个示例之间共享的属性?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题