我试图根据这篇文章使用刚性来测试智能合约的要求:
http://truffleframework.com/tutorials/testing-for-throws-in-solidity-tests
这是合同,抛出代理合同和测试:
/* 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
答案 0 :(得分:2)
让我们看看您的代码:
function storeNum(uint mynum)
public
returns (bool success)
{
require(mynum > 10);
mynumber = mynum;
return true;
}
通过定义returns (bool success)
,您可以向solidity编译器说两件事:
success
的变量bool
(变量定义)但是你的变量毕竟没有返回(你的函数以return true
结束,这就是你在测试中得到InvalidResponse错误的原因。
对于您的代码,最有效的代码是:
function storeNum(uint mynum)
public
returns (bool) // you should define at least the type
{
require(mynum > 10);
mynumber = mynum;
return true;
}
作为练习,使用变量success
:
function storeNum(uint mynum)
public
returns (bool success)
{
require(mynum > 10);
mynumber = mynum;
success = true; // don't need to use return
}
希望它有所帮助。
答案 1 :(得分:0)
好吧,我已经向Truffle github提交了一个问题,它仍然是打开的,也许这与最近的更改有关:
https://github.com/trufflesuite/truffle/issues/1001
目前的解决方法是将函数包装到void函数中,并测试执行是否成功:
/*
Contract wrapper that inherits from MyContract. This contract wraps the calls of non void functions when
testing for exceptions.
Why not invoke directly the function?: https://github.com/trufflesuite/truffle/issues/1001
*/
contract MyExposedContract is MyContract() {
function callStoreNum(uint256 num) public {
storeNum(num);
}
}
然后在测试中使用该包装器
function testTheThrow() {
MyExposedContract mycontract = new MyExposedContract ();
ThrowProxy throwproxy = new ThrowProxy(address(mycontract));
uint num = 7;
MyExposedContract (address(throwproxy)).callStoreNum(num);
bool r = throwproxy.execute.gas(200000)();
Assert.isFalse(r, "Should be false because it should throw!");
}