坚固性:用华夫饼 + 柴测试还原的断言不起作用



我正在测试ERC721令牌的智能合约,该令牌继承自OpenZeppelin,使用NodeJS v14.18.0的waffle和chai,但chai的还原断言似乎没有按预期运行。

依赖性:

"devDependencies": {
"@nomiclabs/hardhat-ethers": "^2.0.1",
"@nomiclabs/hardhat-etherscan": "^2.1.0",
"@nomiclabs/hardhat-waffle": "^2.0.1",
"@typechain/ethers-v5": "^5.0.0",
"@types/chai": "^4.2.14",
"@types/mocha": "^8.2.0",
"@types/node": "^14.14.14",
"chai": "^4.2.0",
"dotenv": "^8.2.0",
"ethereum-waffle": "^3.2.1",
"ethers": "^5.0.24",
"hardhat": "^2.0.5",
"hardhat-typechain": "^0.3.4",
"ts-generator": "^0.1.1",
"ts-node": "^9.1.1",
"typechain": "^4.0.1",
"typescript": "^4.1.3"
},

示例代码:

let genesisTokenHolders: Array<string>;
beforeEach(async () => {
genesisTokenHolders = new Array(230).fill('0x0000000000000000000000000000000000000000')
genesisTokenHolders[1] = addr1.address;
genesisTokenHolders[2] = addr2.address;
})
describe('airdrop genesis tokens', async () => {
it('should fail when non-owner calls the function', async () => {

let addGenesisHoldersTx = await myToken.addGenesisTokenHolderAddresses(genesisTokenHolders)
await addGenesisHoldersTx.wait();

await expect(await myToken.connect(addr1).airdropTokensToGenesisHolders(1, 2)).to.be.revertedWith("Ownable: caller is not the owner");
})

it('should succeed when called by the owner', async () => {
let addGenesisHoldersTx = await myToken.addGenesisTokenHolderAddresses(genesisTokenHolders)
await addGenesisHoldersTx.wait();

let airdropTx = await myToken.connect(deployer).airdropTokensToGenesisHolders(1,2);
await airdropTx.wait();

// addr1 should own tokenID 1, addr2 should own tokenId 2
await expect(await myToken.ownerOf(1)).to.hexEqual(addr1.address)
await expect(await myToken.ownerOf(2)).to.hexEqual(addr2.address)

await expect(await myToken.ownerOf(1000)).to.be.reverted;
})
});

尽管使用了.to.be.revertedWith().to.be.reverted,测试套件仍然抛出错误:

airdrop genesis tokens
should fail when non-owner calls the function:
Error: VM Exception while processing transaction: revert Ownable: caller is not the owner
airdrop genesis tokens
should succeed when called by the owner:
Error: VM Exception while processing transaction: revert ERC721: owner query for nonexistent token

预期结果:测试应通过。

不要等待承诺在expect内解决

改变:

await expect(await myToken.ownerOf(1000)).to.be.reverted;

:

await expect(myToken.ownerOf(1000)).to.be.reverted;

相关内容

  • 没有找到相关文章

最新更新