我有以下合同:
pragma solidity ^0.4.17;
//import "github.com/ethereum/dapp-bin/library/stringUtils.sol";
import "github.com/OpenZeppelin/zeppelin-solidity/contracts/ownership/Ownable.sol";
contract TodoList is Ownable {
event NewTodo(uint todoId, string value);
event DeleteTodo(uint todoId, string value);
//every user has an array of todo items
mapping(uint => address) todoOwner;
//every address has a certain number of todos on it
mapping(address => uint) ownerTodoCount;
TodoItem[] public todoItems;
struct TodoItem {
string value;
bool active;
}
function createTodo(string _value) public returns(uint) {
uint id = todoItems.push(TodoItem(_value, true)) - 1;
todoOwner[id] = msg.sender;
ownerTodoCount[msg.sender]++;
NewTodo(id, _value);
return id;
}
}现在我想写一个关于混合的测试来检查合同的行为。
pragma solidity ^0.4.17;
import "github.com/trufflesuite/truffle/build/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "./TodoList.sol";
contract TestTodoList {
TodoList todolist = TodoList(DeployedAddresses.TodoList());
// Testing the adopt() function
function testUserCanAddTodo() public {
uint returnedTodo = todolist.createTodo("test");
uint expectedValue = 1;
Assert.equal(returnedTodo, expectedValue, "Todo should be added to the list and return 1.");
}
}您可以从github导入Assert.sol文件,但是要导入t imported theDeployableContract.sol`文件,因为这似乎是由松露自动生成的。
有什么建议如何仍然在混合浏览器IDE中编写我的测试?
谢谢你的答复
发布于 2018-02-02 09:51:27
在Remix里测试你的合同应该没有问题,但我认为你会放松特弗莱公司提供的功能,比如自动清洁环境准备。要回答关于DeployableContract.sol的问题:
图片中根本没有松露,所以'DeployableContract.sol‘是不相关的。您必须手动管理您的合同的实例化,使用
contract TestTodoList { TodoList todolist = new TodoList(); ...
如果您希望为每个测试提供新的TodoList实例(可能是这种情况)或
contract TestTodoList { TodoList todolist; function TodoList(address addr) public { todoList = TodoList(addr); } ...
如果您正在针对某些现有实例运行测试。
我在里米克斯尝试过你的密码。
首先,它抱怨库部署错误。如果将Assert.sol从github复制到浏览器(并更改导入import "./Assert.sol"),则可以解决此问题。
接下来,它抱怨必须支付构造函数。在这种情况下,我不太清楚这个错误到底意味着什么。也许其他人可以详细说明。希望这能有所帮助。
https://ethereum.stackexchange.com/questions/38280
复制相似问题