Don't understand why a test is passing #3188
-
Hello everyone! A little ask for help understanding. I'm doing lesson 16 of Foundry Fundamentals (Adding more coverage to the tests). As I was coding along Patrick, Cursor suggested the following: function testOnlyOwnerCanWithdraw() public {
vm.prank(USER);
fundMe.fund{value: SEND_VALUE}();
vm.expectRevert();
fundMe.withdraw();
} and Patrick did this: function testOnlyOwnerCanWithdraw() public {
vm.prank(USER);
fundMe.fund{value: SEND_VALUE}();
vm.prank(USER);
vm.expectRevert();
fundMe.withdraw();
} I thought my test should fail because vm.prank(USER) only works for the immediate next tx. But it passed. function testOnlyOwnerCanWithdraw() public {
// See who's who
console.log("Owner of contract:", fundMe.i_owner());
console.log("Test contract address:", address(this));
console.log("USER address:", USER);
// Fund the contract
vm.prank(USER);
console.log("Funding from address:", msg.sender);
fundMe.fund{value: SEND_VALUE}();
// Try to withdraw
vm.expectRevert();
console.log("Trying to withdraw from address:", msg.sender);
fundMe.withdraw();
} and I got this: Logs:
Owner of contract: 0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38
Test contract address: 0x7FA9385bE102ac3EAc297483Dd6233D62b3e1496
USER address: 0xF921F4FA82620d8D2589971798c51aeD0C02c81a
Funding from address: 0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38
Trying to withdraw from address: 0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38
If the owner and the address trying to withdraw are the same the transaction is not reverting. Why is it still passing the test? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 9 replies
-
Great question. I'm doing the exact same lesson actually 😄 The function test_Owner_IsMsgSender() public view {
assertEq(fundMe.i_owner(), msg.sender);
} If you want to know who's actually calling the Include a log in the modifier onlyOwner() {
console.log("Caller address of onlyOwner / withdraw():", msg.sender, "\n");
if (msg.sender != i_owner) revert NotOwner();
_;
} and in the function fund() public payable {
console.log("Actual msg.sender in fund():", msg.sender, "\n");
...
}
function withdraw() public onlyOwner {
console.log("Actual msg.sender in withdraw():", msg.sender, "\n");
...
} If I don't prank anything before vm.expectRevert();
fundMe.withdraw(); The test passes because the
If I prank with Alice, vm.prank(alice);
vm.expectRevert();
fundMe.withdraw(); the test also passes because Alice is calling the
And finally, if I prank with vm.prank(fundMe.i_owner());
vm.expectRevert();
fundMe.withdraw(); the test fails because it is the owner that calls the
|
Beta Was this translation helpful? Give feedback.
Great question. I'm doing the exact same lesson actually 😄
The
msg.sender
in the console logs is actually thefundMe.i_owner()
that we tested in the second test:If you want to know who's actually calling the
fund()
andwithdraw()
functions, you need to log them inside the functions themselves. Themsg.sender
is just the caller of the test function, which we already established is the owner.Include a log in the
onlyOwner()
modifier: