Foundry Testing – Differences in Pranking Between Test Contracts

foundrytesttesting

I have a test in foundry where I prank msg.sender as such:

address USER = address(1);
vm.startPrank(USER);
myContract.doStuff();
console.log("msg.sender from test", msg.sender);
vm.stopPrank();

And in myContract, I have:

function doStuff() public {
    console.log("msg.sender from contract", msg.sender);

And as an output, I get:

msg.sender from test 0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38
msg.sender from contract 0x0000000000000000000000000000000000000001

Is this correct? Why is msg.sender not being pranked in my test?

Best Answer

Your understanding is correct. The prank cheatcode only applies to calls made from your tests; you cannot use the prank cheatcodes to change the msg.sender of the test functions themselves.

To quote the Foundry Book (emphasis mine):

Sets msg.sender to the specified address for the next call.

Worth noting that you can change the default msg.sender used for test functions by setting the sender option in your Foundry config:

# foundry.toml
[profile.default]
sender = "0xcafe..."
Related Topic