我正在使用 Truffle 和 TestRPC 开发以太坊合约。但是我无法获取要更新的状态变量。我认为可能只是我访问它太早了,但其他示例测试似乎工作得很好并且非常相似。
我已将我的合同缩减为最简单的可能破坏的事情:
pragma solidity ^0.4.11;
contract Adder {
uint public total;
function add(uint amount) {
total += amount;
}
function getTotal() returns(uint){
return total;
}
}
这是我的测试:
var Adder = artifacts.require("./Adder.sol");
contract('Adder', accounts => {
it("should start with 0", () =>
Adder.deployed()
.then(instance => instance.getTotal.call())
.then(total => assert.equal(total.toNumber(), 0))
);
it("should increase the total as amounts are added", () =>
Adder.deployed()
.then(instance => instance.add.call(10)
.then(() => instance.getTotal.call())
.then(total => assert.equal(total.toNumber(), 10))
)
);
});
第一次测试通过了。但第二次测试失败,因为getTotal
仍然返回 0。