How to Test A Solidity Event
In this tutorial, you'll learn how to use Foundry's 'cheatcodes'—special commands that allow you to test and manipulate blockchain states. We'll focus on the `vm.expectEmit` cheatcode to test events.
What you will accomplish
Prerequisites
Before you begin, you should be familiar with the following:
Get started
Set up project
To follow along, start with the main
branch, which is the default branch of this repository. This gives you the initial state from which you can follow along with the steps as described in the tutorial.
Learn how to setup foundry by completing the Setup Foundry and Write a Basic Unit Test tutorial. This tutorial will not walkthrough setting up Foundry.
forge
manages dependencies by using git submodules. Clone the following project and pass --recurse-submodules
to the git clone
command to automatically initialize and update the submodule in the repository.
Install the submodule dependencies
Open the project test-an-event-with-foundry
, in a code editor.
If you completed the previous tutorial, you may notice the contents of the contract TodoList.sol
have changed. Specifcally, there is a CreateTodo event that has been declared and is emitted in the createTodo()
function.
Write the test
An almost-complete test has already been prepared for you. It's located at test/TodoList.t.sol
. You will only need to make a few modifications (outlined below) for it to run successfully.
Look for a comment in the code to locate the specific lines of code which you will need to edit. For example, in this step, look for this: // Step (1) in the accompanying tutorial You will need to delete the inline comment that looks like this: /* ... */. Replace it with the correct code.
Step 1: Define the expected event
Declare the CreateTodo
event in your test contract. This event is identical to the one that is declared in TodoList.sol
.
Step 2: Get the current number of todos
Grab the number of todos, as it will be used to create the expected event according to the current state of the contract.
Step 3: Specify the event data to test
The cheatcode vm.expectEmit()
will be used to check if the event is emitted. This cheatcode expects four inputs:
bool checkTopic1
asserts the first indexbool checkTopic2
asserts the second indexbool checkTopic3
asserts the data for index 3bool checkData
asserts the remaining data emitted by the eventaddress emitter
asserts the emitting address matches
The event being tested includes two indexed arguments: address indexed creator
and uint256 indexed todoIndex
. Therefore, we want to assert the matching of topic 1, topic 2, the non-indexed data, and the emitting address with our actual event.
Step 4: Emit the expected event
Emit the expected event with the following parameters:
the
creator
of the todo is this test contract with address0x7FA9385bE102ac3EAc297483Dd6233D62b3e1496
,the
todoIndex
is the currentnumberOfTodos
+ 1,the
description
is set to "a new todo."
Step 5: Execute the contract function that emits the event
Execute TodoList.sol
's createTodo()
function.
Run the test
Step 6: Execute the test
You should see output similar to the following:
The test passed and shows the event is working as expected.
Complete
Congratulations, you have completed how to test a solidity event using Foundry. You have learned how to:
Last updated