1

I have a very simple contract function (bogusFunc) that makes use of the implicit msg.sender:

function bogusFunc(uint256 _val) public
{
    // use _val for something
    user_list.push(msg.sender);
}

function isUser(address _address) public view returns (bool) {
    for (uint256 s = 0; s < user_list.length; s += 1){
        if (_address == user_list[s]) return (true);
    }
    return (false);
}

Function works fine from truffle console. Now I want to create a unit test from the remix UI:

function beforeAll() public {
    // Here should instantiate tested contract
    foo = new Storage();
}

function checkBogus() public {
    foo.bogusFunc(1);
    Assert.equal(foo.isUser(msg.sender), true, "Could not add user");
}

When running the test, this fails (isUser(msg.sender) returns false).

Now, if I make the function with explicit msg.sender:

function bogusFunc(uint256 _val, address _address)
    public
{
    // use _val for something
    user_list.push(_address);
}

And modify the unit test:

function checkAdd() public {
    foo.bogusFunc(1, msg.sender);
    Assert.equal(foo.isUser(msg.sender), true, "Could not add user");
}

This works fine! It seems like when executing remix testing, the contract function runs without loading values for msg.* ... is there any way to specify that in the tests? or maybe I am overseeing or omitting something?

Thanks!

2
  • Sorry I found the documentation in the REMIX-IDE readthedocs Basically you need to do acc0 = TestsAccounts.getAccount(0); when initializing the tests (beforeAll()) and then acc0 becomes msg.sender
    – Neurus
    Jan 25, 2021 at 4:59
  • Turns out acc0 = TestsAccounts.getAccount(0); doesn't work either... it gives a different address as msg.sender in the contract ....
    – Neurus
    Jan 25, 2021 at 6:16

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.