[Ethereum] DeployedAddresses.sol – Test contracts on remix

remixsolidity

I have the following contract:

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;
    }
}

Now I would like to write a test on remix to check the behavior of the contract.

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.");
    }

}

As you can I imported the Assert.sol file from github, but havent imported theDeployableContract.sol` file, as it seems that this is automatically generated by truffle.

Any suggestions how to still write my test within the remix browser IDE?

I appreciate your replies

Best Answer

There should be no problem to test your contracts in Remix, but I think you will loose features offered by Truffle, like automatic clean-room environment preparation. To answer you question about DeployableContract.sol:

There will be no truffle in the picture at all, so 'DeployableContract.sol' is irrelevant. You will have to manage instantiation of your contracts manually, with

contract TestTodoList { TodoList todolist = new TodoList(); ...

if you want new instance of TodoList for each test (probably the case) or

contract TestTodoList { TodoList todolist; function TodoList(address addr) public { todoList = TodoList(addr); } ...

if you are running test against some existing instance.

I have tried your code in Remix.

First, it complains about library deployment error. If you copy Assert.sol from github into browser (and change import import "./Assert.sol") you can work around this problem.

Next, it complains about a constructor having to be payable. I am not quite sure what this error means exactly in this context. Maybe others can elaborate. Hope this helps.

Related Topic