Solidity

remix 上的單元測試不斷拋出 typeError

  • August 12, 2021

我正在嘗試為我的代幣合約編寫一個單元測試,它的建構子需要 6 個參數。我已經編寫了測試,但是每次我嘗試測試代幣合約上的任何函式時,它都會拋出 TypeError 說“Member”

$$ functionName $$“在類型中依賴於參數的查找後未找到或不可見”。這是單元測試程式碼

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.22 <0.9.0;

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

// This import is required to use custom transaction context
// Although it may fail compilation in 'Solidity Compiler' plugin
// But it will work fine in 'Solidity Unit Testing' plugin
import "remix_accounts.sol";
import "./Bet.sol";

contract testSuite is Bet{
   constructor() Bet(TestsAccounts.getAccount(0), TestsAccounts.getAccount(0), TestsAccounts.getAccount(0), TestsAccounts.getAccount(0), TestsAccounts.getAccount(0), TestsAccounts.getAccount(0)) payable {
       
   }


    /// 'beforeAll' runs before all other tests
   /// More special functions are: 'beforeEach', 'beforeAll', 'afterEach' & 'afterAll'
   function beforeAll() public { 
       Assert.equal(testSuite.totalSupply(), 250000000 * 10 ** 8, "Total supply should be 250 
          million");
   }
}

請,我可以如何解決這個問題的任何幫助?

單元測試的正確方法是使用外部呼叫測試合約,這樣您可以看到呼叫參數和返回值,然後決定您應該使用什麼結構。如果您想測試內部/私有功能,請將它們公開僅用於單元測試,在驗證您的程式碼後將它們設為內部/私有。我認為從測試程式碼繼承測試程式碼時,您會為自己製造混亂,將來很難維護。我說的不僅僅是 Solidity,這也適用於其他語言。

我試圖在下面重新創建您的場景

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;

// TESTED Contract
contract Bet{
   // For convenience
   event Hello(
     address acc0, address acc1, address acc2,
     address acc3,address acc4, address acc5);
       
   constructor(
       address acc0, 
       address acc1,
       address acc2,
       address acc3,
       address acc4,
       address acc5
   ) payable {
       emit Hello(acc0, acc1, acc2, acc3, acc4,acc5);
   }
   
   function totalSupply() public pure returns (uint256)
   {
       return 250000000 * 10 ** 8;
   }

}

測試員契約是:

// SPDX-License-Identifier: GPL-3.0
   
pragma solidity >=0.4.22 <0.9.0;

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

// This import is required to use custom transaction context
// Although it may fail compilation in 'Solidity Compiler' plugin
// But it will work fine in 'Solidity Unit Testing' plugin
import "remix_accounts.sol";
import "./Bet.sol";

// File name has to end with '_test.sol', this file can contain more than one testSuite contracts
contract testSuite {
   
   // Instance of the tested contract
   Bet bet = new Bet( 0x92154D3Ca6b7E34aC0F824c42a7cC18A495cabaB,
       0x92154D3Ca6b7E34aC0F824c42a7cC18A495cabaB,
       0x92154D3Ca6b7E34aC0F824c42a7cC18A495cabaB,
       0x92154D3Ca6b7E34aC0F824c42a7cC18A495cabaB,
       0x92154D3Ca6b7E34aC0F824c42a7cC18A495cabaB,
       0x92154D3Ca6b7E34aC0F824c42a7cC18A495cabaB
       );

   function beforeAll() public {
       Assert.equal(bet.totalSupply(), 250000000 * 10 ** 8, "Total supply should be 250 million");
   }

   function checkFailure() public {
       Assert.notEqual(bet.totalSupply(), 250000000 * 10 ** 7, "Total supply should not be 250 thousand");
   }
}

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