Solidity-0.5.x

Remix 記錄的測試 test#2 失敗

  • January 7, 2022

為什麼混音的記錄測試#2 失敗?嗨,我正在關注 remix 的範例測試: https ://remix-ide.readthedocs.io/en/latest/unittesting_examples.html 。發件人程式碼是:

pragma solidity ^0.5.3;
contract sender {
   address private owner;
   constructor() public {
   owner = msg.sender;
   }
   function updateOwner(address newOwner) public {
       require(msg.sender == owner, "only current owner can update owner");
       owner = newOwner;
   }
   function getOwner() public view returns (address) {
       return owner;
   }
}

//測試文件是:

pragma solidity 0.5.3;

// This import is automatically injected by Remix
import "remix_tests.sol"; 
import "tests/sender.sol";
import "remix_accounts.sol";


   
   // File name has to end with '_test.sol', this file can contain more than one testSuite contracts
   contract testSuite is sender{
       //sender obj;
       address acc0;
       address acc1;
       address acc2;
       function beforeAll() public {
           acc0 = TestsAccounts.getAccount(0); 
           acc1 = TestsAccounts.getAccount(1);
           acc2 = TestsAccounts.getAccount(2);
       }
       function testInitialOwner() public {
          Assert.equal(getOwner(), acc0, 'owner should be acc0');
       }
       function updateOwnerOnce() public {
           // check method caller is as expected
           Assert.ok(msg.sender == acc0, 'caller should be default account i.e. acc0');
           // update owner address to acc1
          updateOwner(acc1);
           // check if owner is set to expected account
          Assert.equal(getOwner(), acc1, 'owner should be updated to acc1');
       }
       function updateOwnerOnceAgain() public {
           // check if caller is custom and is as expected
           Assert.ok(msg.sender == acc1, 'caller should be custom account i.e. acc1');//This is failing
       }
   }

我得到以下輸出:

testSuite (tests/sender_test.sol)
✓ Test initial owner
✓ Update owner once
✘ Update owner once again
Error Message:
"caller should be custom account i.e. acc1"
Assertion:
Expected value should be
'true'
Received value:
false
Skipping the remaining tests of the function.
Result for tests/sender_test.sol
Passing: 2
Failing: 1
Total time: 0.30s

我還附上了圖片:

執行 Remix test2 範例的螢幕截圖

有人請指導我,這是什麼問題。

祖爾菲。

評論說我們必須使用與 account0 無關的自定義,而不是與 account1。

這是通過以下方式實現的:

/// #sender: account-1

所以我們會在之前使用上面的語句:

updateOwnerOnceAgain()

祖爾菲。

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