Solidity
Solidity:無法在松露測試中呼叫超級函式可能是因為定義了修飾符
當我使用 truffle test ./test/testinheritance.js 測試我的課程時,我收到了一個錯誤。“錯誤:處理事務時出現 VM 異常:還原”。
這似乎是與必須由所有者呼叫函式 setHeir 的條件有關的錯誤。
我不確定如何在測試腳本中設置/獲取/檢查所有者,以便我可以測試契約。
請幫我通過腳本測試這份契約。測試腳本是
測試腳本是
var DormantAssetFactory = artifacts.require("./DormantAssetFactory.sol"); var DormantAsset = artifacts.require("./DormantAsset.sol"); var dormantAssetAddress = null; contract('DormantAssetFactory', function (accounts) { it("Let's call Heir function contract", async function () { var dormant_asset_factory = await DormantAssetFactory.deployed(); (await dormant_asset_factory.newDormantAsset()); //now get the Address of first contrac /This is not correct way but to show error dormantAssetAddress = (await dormant_asset_factory.getDormantAssetAt(0)); var contractInstance = DormantAsset.at(dormantAssetAddress); //get contract at address //Call function of parent of current contract // console.log(contractInstance); var result = (await contractInstance.setHeir(accounts[1])); //Just temp conditions assert.equal(1, 1, "Yes we called setHeir"); });
});
工廠類是 DormantAsstFactory
pragma solidity ^0.4.18; import './DormantAsset.sol'; contract DormantAssetFactory { // index of created DormantAsset contracts address[] public dormantAssets; uint256 public _heartbeatTimeout =1000000; function getContractCount() public constant returns(uint contractCount) { return dormantAssets.length; } // deploy a new contract function newDormantAsset() public returns(address _newDormantAssetContract) { DormantAsset c = new DormantAsset(_heartbeatTimeout); dormantAssets.push(c); return c; }
}
該合約在工廠內部創建,它繼承自該合約 https://github.com/OpenZeppelin/zeppelin-solidity/blob/master/contracts/ownership/Heritable.sol
契約類別是
pragma solidity ^0.4.18; import 'zeppelin-solidity/contracts/ownership/Heritable.sol'; contract DormantAsset is Heritable { event Sent(address indexed payee, uint256 amount, uint256 balance); event Received(address indexed payer, uint256 amount, uint256 balance); function DormantAsset(uint256 _heartbeatTimeout) Heritable(_heartbeatTimeout) public {} function () public payable { Received(msg.sender, msg.value, this.balance); } function sendTo(address payee, uint256 amount) public onlyOwner { require(payee != 0 && payee != address(this)); require(amount > 0); payee.transfer(amount); Sent(payee, amount, this.balance); }
}
contract DormantAsset
is的所有者,dormant_asset_factory
您正在嘗試setHeir()
從accounts[0]
. 函式setHeir()
有修飾符onlyOwner
,我們已經知道所有者是工廠契約。您應該callSetHeir()
在您的工廠契約中實現功能,然後從您的測試中呼叫它。希望能幫助到你。
***獎勵:***用返回值編寫函式的更易讀的方式
function newDormantAsset() public returns(DormantAsset c) { c = new DormantAsset(_heartbeatTimeout); dormantAssets.push(c); }