question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Solidity Unit Testing: Custom transaction context does not persist into function calls

See original GitHub issue

Docs: https://remix-ide.readthedocs.io/en/latest/unittesting.html

The issue is that while the transaction context works within the test function, it seemingly has no effect when actually calling the contract under test. Here is a minimal example I’ve created to demonstrate the issue:

SampleContract.sol

pragma solidity 0.8.7;

contract SampleContract {
    function debug() external returns (address) {
        return msg.sender;
    }
}

Sample_test.sol

import "remix_tests.sol"; 
import "remix_accounts.sol";

contract SampleTest {
    
    SampleContract s;
    
    function beforeEach() public {
        s = new SampleContract();
    }
    
    /// #sender: account-0
    function account0Works () public {
        Assert.equal(msg.sender, TestsAccounts.getAccount(0), "debug"); //THIS WORKS
        
        address actualSender = s.debug();
        Assert.equal(actualSender, TestsAccounts.getAccount(0), "this should work"); //THIS IS BROKEN
    }
    
    /// #sender: account-1
    function account1IsBroken () public {
        Assert.equal(msg.sender, TestsAccounts.getAccount(1), ""); //THIS WORKS
        
        address actualSender = s.debug();
        Assert.equal(actualSender, TestsAccounts.getAccount(1), "this should work"); //THIS IS BROKEN - actualSender is the same as previous test case
    }
}

As always, this could easily be a case of operator error, so any suggestions or advice is appreciated.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

3reactions
techiejdcommented, Mar 8, 2022

Ok for any one going down this rabbit hole, here is what changes need to be made:

  1. Make the test an instance of the contract to be tested.
  2. All calls need to be internal on the particular. So, all external functions need to be public so that you can call it internally w/o using contract.func and/or this.func which actually creates a new message and sets something else as the sender.
0reactions
techiejdcommented, Mar 8, 2022

What exactly did you change? I’m having the same issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unit test on remix keeps throwing typeError
1 Answer 1 ... Proper approach to the unit testing is test contract with external calls, in this way you can see the...
Read more >
Remix Unit Testing feature request: override/specify msg ...
Remix unit testing plugin currently supports custom transaction context, where msg.sender and msg.value can be specified in the comments.
Read more >
Solidity Unit Testing with Remix IDE — A Few Missing Pieces
As I wanted to test only the manager can call the addVoter function, I had to use the custom transaction context. However, it...
Read more >
Solidity function is not updating the value in the file
You are calling the method with .call() instead of making transaction with .send() . This should resolve your failing test.
Read more >
Write Solidity tests - Truffle Suite
Note that Truffle sends Ether to your test contract in a way that does not execute a fallback function, so you can still...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found