Remix

測試 Remix example2 合約

  • December 28, 2021

我正在嘗試在以下位置執行混音測試#2: Testing the Remix example contract#2 但我得到了錯誤:

tests/senderTest_test.sol:33:22: DeclarationError: Undeclared identifier.
Assert.equal(getOwner(), acc0, 'owner should be acc0');
^------^

發送者合約是:

pragma solidity >=0.4.22 <0.7.0;
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;
   }
}
//and the tester contract is:
pragma solidity >=0.4.22 <0.9.0;

// 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 {
   //sender obj;
   address acc0;
   address acc1;
   address acc2;
   /// 'beforeAll' runs before all other tests
   /// More special functions are: 'beforeEach', 'beforeAll', 'afterEach' & 'afterAll'
   function beforeAll() public {
       // <instantiate contract>
       //Assert.equal(uint(1), uint(1), "1 should be equal to 1");
       acc0 = TestsAccounts.getAccount(0); 
       acc1 = TestsAccounts.getAccount(1);
       acc2 = TestsAccounts.getAccount(2);
       //obj = new sender();
   }
   function testInitialOwner() public {
       // account at zero index (account-0) is default account, so current owner should be acc0
       //Assert.equal(obj.getOwner(), acc0, 'owner should be acc0');
        Assert.equal(getOwner(), acc0, 'owner should be acc0');
   }
}

有人請指導我。

祖爾菲。

問題出在getOwner(). 它是合約發送方的功能,但您嘗試在testSuite. testSuite不知道它是什麼getOwner()以及它來自哪裡。我在您正確使用它之前的行中看到了obj.getOwner(). obj是類型sender,因此知道如何使用getOwner().

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