【发布时间】:2018-06-11 21:59:09
【问题描述】:
我正在尝试根据本文仅使用可靠性来测试智能合约的要求:
http://truffleframework.com/tutorials/testing-for-throws-in-solidity-tests
这是合约,throw代理合约和测试:
/* Testing with solidity tests. */
import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../contracts/MyContract.sol";
contract TestMyContract {
function testInitialStoredValue() {
MyContract mycontract = new MyContract();
uint expected = 24;
Assert.equal(mycontract.mynumber(), expected, "First number set should be 24.");
}
function testTheThrow() {
MyContract mycontract = new MyContract();
ThrowProxy throwproxy = new ThrowProxy(address(mycontract));
uint num = 7;
MyContract(address(throwproxy)).storeNum(num);
bool r = throwproxy.execute.gas(200000)();
Assert.isFalse(r, "Should be false because is should throw!");
}
function testNoThrow() {
MyContract mycontract = new MyContract();
ThrowProxy throwproxy = new ThrowProxy(address(mycontract));
MyContract(address(throwproxy)).storeNum(22);
bool r = throwproxy.execute.gas(200000)();
Assert.isTrue(r, "Should be true because is should throw!");
}
}
// Proxy contract for testing throws
contract ThrowProxy {
address public target;
bytes data;
function ThrowProxy(address _target) {
target = _target;
}
//prime the data using the fallback function.
function() {
data = msg.data;
}
function execute() returns (bool) {
return target.call(data);
}
}
如果我运行测试,我会收到此错误:
如果我将 storeNum 函数更改为 void
function storeNum(uint mynum)
public
returns (bool)
{
require(mynum > 10);
mynumber = mynum;
return true;
}
到
function storeNum(uint mynum)
public
{
require(mynum > 10);
mynumber = mynum;
return true;
}
测试有效..
有什么想法吗?
我正在使用 Truffle v4.1.11
【问题讨论】:
标签: testing ethereum solidity truffle