Video 9, Event testing, timestamp 4:30, test does not pass #6
-
Hi, First thank you very much for your online tutorial videos. I am going through your foundry playlist. Now I'm at the 9th video, where you teach how to test events. At timestamp 4:30 you run the first test, it passes, I run the test, and it fails. Forge output: $ forge test --mt testEmitTransferEvent -vvv
[⠊] Compiling...
No files changed, compilation skipped
Ran 1 test for test/Event.t.sol:EventTest
[FAIL: log != expected log] testEmitTransferEvent() (gas: 11992)
Traces:
[11992] EventTest::testEmitTransferEvent()
├─ [0] VM::expectEmit(true, true, false, true)
│ └─ ← [Return]
├─ emit Transfer(from: EventTest: [0x7FA9385bE102ac3EAc297483Dd6233D62b3e1496], to: 0x000000000000000000000000000000000000007B, amount: 456)
├─ [2291] Event::transfer(EventTest: [0x7FA9385bE102ac3EAc297483Dd6233D62b3e1496], 0x000000000000000000000000000000000000007B, 456)
│ ├─ emit Transfer(from: EventTest: [0x7FA9385bE102ac3EAc297483Dd6233D62b3e1496], to: 0x000000000000000000000000000000000000007B, amount: 456)
│ └─ ← [Stop]
└─ ← [Revert] log != expected log
Suite result: FAILED. 0 passed; 1 failed; 0 skipped; finished in 338.52µs (74.09µs CPU time)
Ran 1 test suite in 514.53ms (338.52µs CPU time): 0 tests passed, 1 failed, 0 skipped (1 total tests)
Failing tests:
Encountered 1 failing test in test/Event.t.sol:EventTest
[FAIL: log != expected log] testEmitTransferEvent() (gas: 11992)
Encountered a total of 1 failing tests, 0 tests succeeded My Event.sol: // SPDX-License-Identifier: MIT
pragma solidity 0.8.20;
contract Event {
event Transfer(address indexed from, address indexed to, uint256 amount);
function transfer(address from, address to, uint256 amount) external {
emit Transfer(from, to, amount);
}
function transferMany(address from, address[] calldata to, uint256[] calldata amounts) external {
for (uint256 i = 0; i < to.length; i++) {
emit Transfer(from, to[i], amounts[i]);
}
}
} My Event.t.sol: // SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
import {Test} from "forge-std/Test.sol";
import {Event} from "../src/Event.sol";
contract EventTest is Test {
Event public e;
event Transfer(address from, address to, uint256 amount);
function setUp() public {
e = new Event();
}
function testEmitTransferEvent() public {
// function expectEmit(
// bool checkTopic1,
// bool checkTopic2,
// bool checkTopic3,
// bool checkData
// ) external;
// 1. Tell Foundry which data to check
// Check index 1, index 2 and data
vm.expectEmit(true, true, false, true);
// 2. Emit the expected event
emit Transfer(address(this), address(123), 456);
// 3. Call the function that should emit the event
e.transfer(address(this), address(123), 456);
}
function testEmitTransferManyEvent() public {}
}
My full code can be found at: What am I missing? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
OK I noticed that in the test the event didn't mention "indexed" for the first two parameters so changing the line 10 in Event.t.sol to
fixed the problem. |
Beta Was this translation helpful? Give feedback.
OK I noticed that in the test the event didn't mention "indexed" for the first two parameters so changing the line 10 in Event.t.sol to
event Transfer(address indexed from, address indexed to, uint256 amount);
fixed the problem.