我正在为我的合同编写测试用例,由于时间敏感,因此不得不延迟断言检查。 getCompletedCampaigns()将具有截止日期已过的广告系列的地址。
it("Time sensitive check", async () => {
var deadLine = Math.round(Date.now() / 1000) + 3;
let eventDetails = await contract.createCampaign("Campaign name",deadLine,
{
from: accounts[0]
});
addressFromEvent = eventDetails['logs'][1]['args']['campaignAddress'];
async function checker() {
let deployedCampaigns = await factory.getCompletedCampaigns();
assert.equal(addressFromEvent, deployedCampaigns[0]);
}
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function sleep() {
await timeout(5000);
return checker();
}
sleep();
});
即使断言应该失败,测试也会通过。该断言将在测试套件完成所有测试并强制提示符从truffle develop
控制台中出来之后进行,因为如果失败了。在下面的测试中,我故意使测试失败。
Contract: Testing CrowdCoin
✓ CampaignFactory deployment
✓ Create a new Campaign (168ms)
✓ Get ongoing Campaigns (246ms)
✓ Get completed Campaigns (189ms)
4 passing (1s)
truffle(develop)>
/home/vagrant/code/test/crowdcoin.test.js:82
assert.equal(0, deployedCampaigns[1]);
^
AssertionError: expected 0 to equal '0x806ea81c279b6000b9fd9f14d2845dec87fc3544'
at checker (/home/vagrant/code/test/crowdcoin.test.js:82:11)
at process._tickCallback (internal/process/next_tick.js:68:7)
我如何确保测试检查随时间而发生?
答案 0 :(得分:2)
unzip_file_command ; process_files_command ; delete_unneeded_files > /dev/null 2>&1 &
通过将整个延迟逻辑移到不同的方法,并且所有逻辑语句都以await开头,除非timeout()已完成,否则不会执行所述逻辑语句和测试完成。
答案 1 :(得分:0)
实际上,您可以尝试在 time
中使用 @openzeppelin/test-helper
来加速您的测试用例,因为您无需在此测试用例上等待 3 秒运行。
const { time } = require("@openzeppelin/test-helpers");
it("Time sensitive check", async () => {
let duration = time.duration.seconds(3);
let eventDetails = await contract.createCampaign("Campaign name",duration,
{
from: accounts[0]
});
await time.increase(duration);
addressFromEvent = eventDetails['logs'][1]['args']['campaignAddress'];
let deployedCampaigns = await factory.getCompletedCampaigns();
await assert.equal(addressFromEvent, deployedCampaigns[0]);
});
在区块链中测试时间戳更有意义。
文档:https://docs.openzeppelin.com/test-helpers/0.5/api#time
@openzeppelin 团队如何处理测试用例的示例,您可以查看: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/24a0bc23cfe3fbc76f8f2510b78af1e948ae6651/test/token/ERC20/utils/TokenTimelock.test.js