我正在用 chai 和 hardhat 测试我的合同,我想在Transactions
块测试中测试一个依赖于 if 语句的函数:bool(Date.now() < timestamp)。
我检查了:如何在 Mocha 测试中模拟时间的流逝,以便调用 setTimeout 回调?
我当然安装了 sinon:yarn add sinon
但我得到:
TypeError: Cannot read property 'useFakeTimers' of undefined
代码:
const { expect } = require("chai");
const { ethers } = require("hardhat");
const { sinon } = require("sinon");
describe("TokenContract", function () {
var tubblySupply = 55_000_000;
var unfreezeTime = Date.now() + 100;
var seed_tokenPrice = 1000000000000000;
var firstRoundTokensAllocation = 500000;
var firstRoundTime = Date.now() + 20;
let tubblySale;
let tubblyToken;
let owner;
let addr1;
let addr2;
beforeEach(async function () {
TubblyToken = await ethers.getContractFactory("TubblyToken");
TubblySale = await ethers.getContractFactory("TubblySale");
[owner, addr1, addr2] = await ethers.getSigners();
tubblyToken = await TubblyToken.deploy(55_000_000, unfreezeTime);
tubblySale = await TubblySale.deploy(tubblyToken.address, seed_tokenPrice, firstRoundTime, firstRoundTokensAllocation);
});
describe("Deployment", function () {
it("Checks the totalSupply", async function() {
expect(await tubblyToken.totalSupply()).to.equal(55_000_000);
});
it("Checks unfreeze time", async function() {
expect(await tubblyToken.unfreezeTime()).to.equal(unfreezeTime);
});
it("Checks owner", async function() {
expect(await tubblyToken.admin()).to.equal(owner.address);
});
it("Checks if saleAddress was assigned succesfuly", async function() {
await tubblyToken.assignSaleAddress(tubblySale.address);
expect(await tubblyToken.saleAddress()).to.equal(tubblySale.address);
});
});
describe("Transactions", function() {
it("Checks if transfer can occur before unfreeze but as owner", async function() {
await tubblyToken.transfer(addr1.address, 20, { from: owner.address });
expect(await tubblyToken.balanceOf(addr1.address)).to.equal(20);
});
it("Checks if transfer can occur from non-privileged account before unfreeze", async function() {
try {
await tubblyToken.connect(addr1).transfer(addr2.address, 20);
} catch (err) {
if (err.message === "VM Exception while processing transaction: reverted with reason string 'Fund are frozen.'") {
}}
});
it("Checks if transfer can be done after unfreeze", async function() {
var clock = sinon.useFakeTimers(now);
clock.tick(unfreezeTime);
await tubblyToken.connect(addr1).transfer(addr2.address, 20);
expect(await tubblyToken.balanceOf(addr2.address)).to.equal(20);
})
});
});
describe("TubblySale", function() {
#TODO
})
感谢帮助!