-
Notifications
You must be signed in to change notification settings - Fork 156
PriceIsRightTest fails due to fuzzing + insufficient balance #47
Description
Issue: Fuzz test for PriceIsRight isn’t working as intended
Right now the fuzz test in PriceIsRightTest looks like this:
function testBuy(uint256 amount) public {
vm.deal(address(this), 1000 ether);
if (amount == 1 ether) {
priceIsRight.buy{value: amount}();
} else {
vm.expectRevert();
priceIsRight.buy{value: amount}();
}
}The problem is that Foundry will generate really huge numbers for amount (like 3e48).
When we try to send those as msg.value, the transaction fails before it even hits the contract.
So the test doesn’t actually check our require(msg.value == 1 ether) — it just fails at the EVM level.
Why this matters
- The fuzzing isn’t exploring useful cases.
- We only care about two situations: exactly
1 ether(should succeed) and everything else (should revert). - Letting fuzz run wild across the whole
uint256space just adds noise and failing tests.
What we should do
Two simple fixes would make this more reliable:
-
Add explicit unit tests for the clear cases:
function testBuySuccess() public { vm.deal(address(this), 1 ether); priceIsRight.buy{value: 1 ether}(); } function testBuyFailure() public { vm.deal(address(this), 2 ether); vm.expectRevert(); priceIsRight.buy{value: 2 ether}(); }
-
Keep the fuzz test but constrain the input so it only explores reasonable ranges (say up to
10 ether):function testBuyFuzz(uint256 amount) public { vm.assume(amount <= 10 ether); vm.deal(address(this), amount); if (amount == 1 ether) { priceIsRight.buy{value: amount}(); } else { vm.expectRevert(); priceIsRight.buy{value: amount}(); } }
Summary
Right now fuzzing is breaking because it generates values that don’t make sense for msg.value.
By adding explicit tests and constraining fuzz input, we get the best of both worlds: clear behavior tests and some fuzz exploration without the noise.