Solidity 运行块菌测试时,Assert.equal不可用
我有一个简单的合同,我正试着用块菌为该合同编写坚固性测试。下面是代码示例-Solidity 运行块菌测试时,Assert.equal不可用,solidity,truffle,Solidity,Truffle,我有一个简单的合同,我正试着用块菌为该合同编写坚固性测试。下面是代码示例- File: dummycontract.sol pragma solidity ^0.4.17; contract DummyContract { uint[] public elements; function addElement(uint element) public { elements.push(element); } function getNumb
File: dummycontract.sol
pragma solidity ^0.4.17;
contract DummyContract {
uint[] public elements;
function addElement(uint element) public {
elements.push(element);
}
function getNumberOfElements() public view returns(uint) {
uint numElements = uint(elements.length);
return numElements;
}
}
而测试文件—
pragma solidity ^0.4.17;
import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../contracts/dummycontract.sol";
contract TestZombieFactory {
function testInitialState() public {
DummyContract dummyContract = DummyContract(DeployedAddresses.DummyContract());
uint numberOfElements = dummyContract.getNumberOfElements();
Assert.equal(numberOfElements == 0);
}
}
在运行块菌测试时,我得到以下错误-
TypeError: Member "equal" is not available in type(library Assert) outside of storage.
Assert.equal(numberOfElements == 0);
有人能给我解释一下吗 您使用的
Assert.equal()
不正确。它需要两个值,并在函数中进行比较。函数签名是
function equal(uint A, uint B, string message) constant returns (bool result)
将测试合同更改为以下内容,它将起作用
pragma solidity ^0.4.17;
import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../contracts/dummycontract.sol";
contract TestZombieFactory {
function testInitialState() public {
DummyContract dummyContract = DummyContract(DeployedAddresses.DummyContract());
uint numberOfElements = dummyContract.getNumberOfElements();
Assert.equal(numberOfElements, 0, "Number of elements should be 0");
}
}
您使用的
Assert.equal()
不正确。它需要两个值,并在函数中进行比较。函数签名是
function equal(uint A, uint B, string message) constant returns (bool result)
将测试合同更改为以下内容,它将起作用
pragma solidity ^0.4.17;
import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../contracts/dummycontract.sol";
contract TestZombieFactory {
function testInitialState() public {
DummyContract dummyContract = DummyContract(DeployedAddresses.DummyContract());
uint numberOfElements = dummyContract.getNumberOfElements();
Assert.equal(numberOfElements, 0, "Number of elements should be 0");
}
}
你应该替换
Assert.equal(numberOfElements == 0);
与
你应该替换
Assert.equal(numberOfElements == 0);
与
啊,好吧,我的错。谢谢啊,好吧,我的错。谢谢