Remix's Example2 test fails

Why the remix’s documented test#2 fails?
Hi,
I am following the remix’s example test available at:

Remix Example tests.

The sender code is:

pragma solidity ^0.5.3;
contract sender {
    address private owner;
    constructor() public {
    owner = msg.sender;
    }
    function updateOwner(address newOwner) public {
        require(msg.sender == owner, "only current owner can update owner");
        owner = newOwner;
    }
    function getOwner() public view returns (address) {
        return owner;
    }
}
//The testfile is:
pragma solidity 0.5.3;

// This import is automatically injected by Remix
import "remix_tests.sol"; 
import "tests/sender.sol";
import "remix_accounts.sol";

contract testSuite is sender{
    //sender obj;
    address acc0;
    address acc1;
    address acc2;
    function beforeAll() public {
        acc0 = TestsAccounts.getAccount(0); 
        acc1 = TestsAccounts.getAccount(1);
        acc2 = TestsAccounts.getAccount(2);
    }
    function testInitialOwner() public {
       Assert.equal(getOwner(), acc0, 'owner should be acc0');
    }
    function updateOwnerOnce() public {
        // check method caller is as expected
        Assert.ok(msg.sender == acc0, 'caller should be default account i.e. acc0');
        // update owner address to acc1
       updateOwner(acc1);
        // check if owner is set to expected account
       Assert.equal(getOwner(), acc1, 'owner should be updated to acc1');
    }
    function updateOwnerOnceAgain() public {
        // check if caller is custom and is as expected
        Assert.ok(msg.sender == acc1, 'caller should be custom account i.e. acc1');//This is failing
        }
}

I am getting following output:

testSuite (tests/sender_test.sol)
✓ Test initial owner
✓ Update owner once
✘ Update owner once again
Error Message:
"caller should be custom account i.e. acc1"
Assertion:
Expected value should be
'true'
Received value:
false
Skipping the remaining tests of the function.
Result for tests/sender_test.sol
Passing: 2
Failing: 1
Total time: 0.30s

Somebody please guide me.

Zulfi.

Hi Zulfi,
I believe this is failing because for each test function everything is reset. So what happens in updateOwnerOnce is no longer present when running updateOwnerOnceAgain. For that test to work correctly you would have to update the owner twice within the same method.

Best,
Adrian

1 Like

when is this function called with acc1?

1 Like

Hi my friends @akur8 and @cryptokid,

I changed the code to this:

// SPDX-License-Identifier: GPL-3.0
    
pragma solidity 0.5.3;

// This import is automatically injected by Remix
import "remix_tests.sol"; 
import "tests/sender.sol";

// This import is required to use custom transaction context
// Although it may fail compilation in 'Solidity Compiler' plugin
// But it will work fine in 'Solidity Unit Testing' plugin
import "remix_accounts.sol";
//import "./senderTest.sol";

// File name has to end with '_test.sol', this file can contain more than one testSuite contracts
contract testSuite is sender{
    //sender obj;
    address acc0;
    address acc1;
    address acc2;
    /// 'beforeAll' runs before all other tests
    /// More special functions are: 'beforeEach', 'beforeAll', 'afterEach' & 'afterAll'
    function beforeAll() public {
        // <instantiate contract>
        //Assert.equal(uint(1), uint(1), "1 should be equal to 1");
        acc0 = TestsAccounts.getAccount(0); 
        acc1 = TestsAccounts.getAccount(1);
        acc2 = TestsAccounts.getAccount(2);
        //obj = new sender();
    }
    function testInitialOwner() public {
        // account at zero index (account-0) is default account, so current owner should be acc0
        //Assert.equal(obj.getOwner(), acc0, 'owner should be acc0');
         Assert.equal(getOwner(), acc0, 'owner should be acc0');
    }
    function updateOwnerOnce() public {
        // check method caller is as expected
        Assert.ok(msg.sender == acc0, 'caller should be default account i.e. acc0');
        // update owner address to acc1
        updateOwner(acc1);
        // check if owner is set to expected account
        Assert.equal(getOwner(), acc1, 'owner should be updated to acc1');
    }
    function updateOwnerOnceAgain() public {
        // check if caller is custom and is as expected
        updateOwner(acc1);
        Assert.ok(msg.sender == acc1, 'caller should be custom account i.e. acc1');
        // update owner address to acc2. This will be successful because acc1 is current owner & caller both
        updateOwner(acc2);
        // check if owner is set to expected account i.e. account2
        Assert.equal(getOwner(), acc2, 'owner should be updated to acc2');
    }
}

But now I got following message:

_test.sol)

✓ Test initial owner

✓ Update owner once

✘ Update owner once again

Error Message:

"Transaction has been reverted by the EVM: { "transactionHash": "0x0903549a5f8dbb815848cf9cb7e09fc27a8389fd8d5669d3853a2e1fd5eaf265", "transactionIndex": 0, "blockHash": "0x485c84f698187ddb62186d848e33dd31eac29506cccf5392943952f6e0392340", "blockNumber": 7, "gasUsed": 25754, "cumulativeGasUsed": 25754, "status": false, "to": "0xD7ACd2a9FD159E69Bb102A1ca21C9a3e3A5F771B", "events": {} }"

Result for tests/sender_test.sol
Passing: 2
Failing: 1
Total time: 0.28s

Zulfi

Based on your code and error message, seems like you are trying to call updateOwner after the ownership has been transferred to acc1

1 Like

Hi @YosephKS, Kindly read post 2 of 5.

Zulfi.

maybe it fails on this line

Hi @zak100,
code seems fine, what I could think of here is that msg.sender in not equal to owner when this line is called updateOwner(acc1);