Solidity Unit Testing: Custom transaction context does not persist into function calls
See original GitHub issueDocs: 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:
- Created 2 years ago
- Comments:6
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Ok for any one going down this rabbit hole, here is what changes need to be made:
external
functions need to bepublic
so that you can call it internally w/o usingcontract.func
and/orthis.func
which actually creates a new message and sets something else as the sender.What exactly did you change? I’m having the same issue.