Siddharth
Siddharth

Reputation: 5219

Assert.equal is not available when running Truffle Tests

I have a simple contract and I am trying to write Solidity tests for that contract in Truffle. Below are the code samples -

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

}

And the test file -

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

}

On running truffle test, I get the following error -

TypeError: Member "equal" is not available in type(library Assert) outside of storage.
        Assert.equal(numberOfElements == 0);

Can someone please explain this to me?

Upvotes: 1

Views: 2020

Answers (2)

riccardogiorato
riccardogiorato

Reputation: 61

You should replace

Assert.equal(numberOfElements == 0);

with

Assert.equal(numberOfElements,0);

Upvotes: 2

Adam Kipnis
Adam Kipnis

Reputation: 10971

Your usage of Assert.equal() is not correct. It expects two values and does the comparison within the function. The function signature is

function equal(uint A, uint B, string message) constant returns (bool result)

Change your test contract to the following and it will work.

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

Upvotes: 2

Related Questions