Skip to content

Instantly share code, notes, and snippets.

@PraneshASP
Last active September 7, 2023 07:16
Show Gist options
  • Save PraneshASP/0ec4234a708787e13e2d50bcdb56f382 to your computer and use it in GitHub Desktop.
Save PraneshASP/0ec4234a708787e13e2d50bcdb56f382 to your computer and use it in GitHub Desktop.
Foundry misleading error statement.
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.11;
import "forge-std/src/Test.sol";
contract Counter {
uint256 value = 10;
event Incremented(uint256 _value);
function increment() external {
emit Incremented(++value);
}
}
contract CounterTest is Test {
event Incremented(uint256 _value);
function testIncrement() public {
/// Contract under test
Counter cut = new Counter();
vm.expectEmit(true, true, true, true, address(cut));
/// Note: Reverts with `call reverted` error. Should revert with `Assertion Failed` / `Event mismatch` error
emit Incremented(10);
/// Note: This one works, which indicates that the call doesn't revert actually.
// emit Incremented(11);
cut.increment();
}
}
/// Command: forge test --mt testIncrement -vvv
/// Output:
/**
Running 1 test for test/POC.t.sol:CounterTest
[FAIL. Reason: Expected an emit, but the call reverted instead. Ensure you're testing the happy path when using the `expectEmit` cheatcode.] testIncrement() (gas: 103653)
Traces:
[103653] CounterTest::testIncrement()
├─ [65399] → new Counter@0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f
│ └─ ← 216 bytes of code
├─ [0] VM::expectEmit(true, true, true, true, Counter: [0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f])
│ └─ ← ()
├─ emit Incremented(_value: 10)
├─ [1466] Counter::increment()
│ ├─ emit Incremented(_value: 11)
│ └─ ← ()
└─ ← "Log != expected log"
Test result: FAILED. 0 passed; 1 failed; 0 skipped; finished in 578.71µs
Ran 1 test suites: 0 tests passed, 1 failed, 0 skipped (1 total tests)
Failing tests:
Encountered 1 failing test in test/POC.t.sol:CounterTest
[FAIL. Reason: Expected an emit, but the call reverted instead. Ensure you're testing the happy path when using the `expectEmit` cheatcode.] testIncrement() (gas: 103653)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment