Repository navigation
vm.expectEmit() failing the test. Not working as expected. #812
Answered
by
theirrationalone
alfheimrShiven
asked this question in
Q&A
|
Lesson 12: Defi / Stablecoin I've written a test to check if the Test: On running the test, I got the following traces: I'm sure there's nothing wrong with the DSCEngine.sol's Opening it up here as we need more sets of eyes to take a look at this and see if we can spot what's going wrong here. Thanks! |
Answered by
theirrationalone
Sep 6, 2023
Replies: 1 comment 5 replies
|
Actually position matters... function testIfCollateralDepositedEventIsEmitted() public {
vm.startPrank(USER);
vm.expectEmit(true, true, true, false, address(dscEngine)); // Here, we expect an event to be emitted.
emit CollateralDeposited(USER, weth, COLLATERAL_AMOUNT); // Here, we emitted the desired event. You can consider it as an expected event to be emitted. However, it's still expecting `CollateralDeposited` to be emitted once a real transaction is performed. As you know, `vm.startPrank` ignores checks <-> cheats and considers only real transactions.
// We perform the call
ERC20Mock(weth).approve(address(dscEngine), COLLATERAL_AMOUNT); // Here, it's a real transaction, and `vm.expectEmit` now tries to compare the logs of the event that was emitted by performing this transaction. Since your traces were above, I can see an event emitted during this transaction's performance, and that's `Approval`. So the logs of `CollateralDeposited` != logs of `Approval`.
dscEngine.depositCollateral(weth, COLLATERAL_AMOUNT); // You just need to emit and expect the event right above this line. As an expected result, eventually, the logs of the event emitted by `dscEngine.depositCollateral` will be == logs of the expected event `CollateralDeposited`, and our test will pass. 😉
vm.stopPrank();
}
Correct ✅variant: function testIfCollateralDepositedEventIsEmitted() public {
vm.startPrank(USER);
ERC20Mock(weth).approve(address(dscEngine), COLLATERAL_AMOUNT);
vm.expectEmit(true, true, true, false, address(dscEngine));
emit CollateralDeposited(USER, weth, COLLATERAL_AMOUNT);
dscEngine.depositCollateral(weth, COLLATERAL_AMOUNT);
vm.stopPrank();
};`) 😉 |
5 replies
Answer selected by
alfheimrShiven
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Actually position matters...
Follow the comments...