Contract-Development
從可靠性測試中訪問 testrpc 地址
我正在諮詢 truffle 的文件,了解如何直接可靠地測試合約。
http://truffleframework.com/docs/getting_started/solidity-tests
我想想辦法在我的測試中引用現有的 testrpc 帳戶。就像是:
pragma solidity ^0.4.11; import "truffle/Assert.sol"; import "truffle/DeployedAddresses.sol"; import "../contracts/BAToken.sol"; contract TestBAToken { function testNewBATokenNotFinalized() { address companyFundAddress = addresses[1]; // <-- I'd like this to be a predefined testrpc account address userFundAddress = addresses[2]; uint256 startBlock = block.number - 100; uint256 endBlock = block.number + 100; BAToken ba = new BAToken(companyFundAddress, userFundAddress, startBlock, endBlock); Assert.equal(ba.isFinalized, false, "Token sale shouldn't be finalized upon initialization."); } }
這可能嗎?如果沒有,有人對如何在測試期間引用現有帳戶有任何好的建議嗎?
你可以直接在solidity合約中添加地址而不是
addresses[1]
andaddresses[2]
。如果您不想這樣做,那麼您可以在合約中為地址聲明一些變數,並將它們初始化為建構子的一部分,如下所示。contract TestBAToken { address companyFundAddress; address userFundAddress; function TestBAToken(address address1, address address2) { companyFundAddress = address1; userFundAddress = address2; } function testNewBATokenNotFinalized() { uint256 startBlock = block.number - 100; uint256 endBlock = block.number + 100; BAToken ba = new BAToken(companyFundAddress, userFundAddress, startBlock, endBlock); Assert.equal(ba.isFinalized, false, "Token sale shouldn't be finalized upon initialization."); } }
如果您只想聲明該函式內的地址而不是完整的契約。您可以將它們作為函式的參數傳遞
testNewBATokenNotFinalized
。實施方式取決於您希望契約如何以及它應該做什麼。