Solidity

無法在松露單元測試中將乙太幣發送到應付函式

  • April 12, 2018

我正在調試我的松露堅固性測試中發生的錯誤。我可以將問題縮小到應付修飾符中發生的異常。

我的契約程式碼:

import "zeppelin-solidity/contracts/token/ERC721/ERC721Token.sol";

contract MyToken is ERC721Token { 
 function MyToken() ERC721Token(NAME, SYMB) public payable {
 }
 function purchase(uint256 _tokenId) public payable {
   // all the code in this function is commented out when I do the test
 }
}

我的測試程式碼:

MyToken myToken = MyToken(contractAddress);
myToken.purchase.value(1000000000000000000).gas(1000000000000000000)(0);

例外:

    Error: VM Exception while processing transaction: revert
 at Object.InvalidResponse (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/errors.js:38:1)
 at /usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/requestmanager.js:86:1
 at /usr/local/lib/node_modules/truffle/build/webpack:/~/truffle-provider/wrapper.js:134:1
 at XMLHttpRequest.request.onreadystatechange (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/httpprovider.js:128:1)
 at XMLHttpRequestEventTarget.dispatchEvent (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:64:1)
 at XMLHttpRequest._setReadyState (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:354:1)
 at XMLHttpRequest._onHttpResponseEnd (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:509:1)
 at IncomingMessage.<anonymous> (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:469:1)
 at endReadableNT (_stream_readable.js:1101:12)
 at process._tickCallback (internal/process/next_tick.js:114:19)

我正在添加答案,因為它在上面的評論中,可能對某人有幫助。

由於測試合約沒有餘額,交易已被撤銷。當您需要通過 Solidity 測試聯繫交易時,發送者就是測試合約。要允許測試合約進行交易,您需要設置初始餘額。

uint public initialBalance = 1 乙太幣;// 或任何其他值

此屬性必須添加到您的測試契約中。參考

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