我有两份合同,我写在一个名为 Sum.sol 的文件中。sum 合约调用 Add 合约。我只想测试跨合约调用。如果我没有将两个合约放在一个文件中,使用 truffle 编译 Sum 会失败。但是当我做测试时,结果太奇怪了。我不知道这是怎么发生的。
pragma solidity ^0.4.6;
contract Add{
function sum(uint x, uint y) returns(uint){
return x+y;
}
}
contract Sum{
function func(uint x, uint y) returns(uint){
Add add = new Add();
uint re = add.sum(x,y);
return re;
}
}
然后我在松露中为它写一个测试
contract('Sum', function(accounts) {
it("should return 5 when add 2 and 3", function() {
var sum = Sum.deployed();
return sum.func.call(2,3).then(function(res){
assert.equal(res.valueOf(), 5, "add result is 5");
});
});
});
并使用 truffle 进行测试,结果是:
Compiling Sum.sol...
Contract: Sum
1) should return 5 when add 2 and 3
> No events were emitted
0 passing (455ms)
1 failing
1) Contract: Sum should return 5 when add 2 and 3:
AssertionError: add result is 5: expected '9.1735649321334958107552852973512799782292704141468709142420585807991067901952e+76' to equal 5
at /Users/maiffany/testcoverage/test/add.js:6:14
at process._tickDomainCallback (internal/process/next_tick.js:129:7)