Openzeppelin

OpenZeppelin ERC721 測試如何通過?

  • May 31, 2018

我不明白以下測試如何通過。我在我的應用程序中嘗試過,但失敗了。

入口點是描述'when called by the owner'

在此函式中執行兩個測試案例。在每個測試案例之前,令牌tokenId都被轉移到儲存在this.to. 傳輸令牌後,批准被刪除。

測試案例transfers the ownership of the given token ID to the given address,因為它測試契約的新所有者。

第二個測試總是'clears the approval for the token ID'會失敗,因為函式transferFunctionbeforeEach的 再次被呼叫但令牌不再屬於owner

我想知道測試框架是否應該表現不同並且應該在執行測試案例之前刪除狀態。我的設置沒有發生這種情況(ganache-cli@6.1.0, Truffle v4.1.11 (core: 4.1.11, Solidity v0.4.24 (solc-js), Mocha 2.5.3

beforeEach(async function () {
 this.to = accounts[1];
 await this.token.approve(approved, tokenId, { from: owner });
 await this.token.setApprovalForAll(operator, true, { from: owner });
});

const transferWasSuccessful = function ({ owner, tokenId, approved }) {
 it('transfers the ownership of the given token ID to the given address', async function () {
   const newOwner = await this.token.ownerOf(tokenId);
   newOwner.should.be.equal(this.to);
 });

 it('clears the approval for the token ID', async function () {
   const approvedAccount = await this.token.getApproved(tokenId);
   approvedAccount.should.be.equal(ZERO_ADDRESS);
 });
...
}

const shouldTransferTokensByUsers = function (transferFunction) {
 describe('when called by the owner', function () {
   beforeEach(async function () {
     ({ logs } = await transferFunction.call(this, owner, this.to, tokenId, { from: owner }));
   });
   transferWasSuccessful({ owner, tokenId, approved });
 });

它之所以有效,是因為為每個測試案例創建了一個新的合約實例。beforeEach這發生在執行每個測試案例之前呼叫的外部函式中。

引用自:https://ethereum.stackexchange.com/questions/50098