Writing Custom Tests
This guide helps you write additional tests for the Zaphenath contract using the Foundry testing framework.
π§° Setup
Tests are located in the test/ directory and follow this structure:
contract ZaphenathTest is Test {
Zaphenath public zaph;
function setUp() public {
zaph = new Zaphenath();
}
}
Use vm.prank() to simulate calls from different addresses:
vm.prank(rachel);
zaph.createKey(...);
β Common Tools
| Tool | Description |
|---|---|
vm.prank() |
Simulate msg.sender |
vm.warp() |
Simulate time passing |
vm.expectRevert() |
Expect failure with specific reason |
console.log() |
Debug inside tests |
π§ͺ Test Ideas
- π€ A Writer tries to delete a key
- π A Reader tries to update a key (should fail)
- β± A custodian pings just before timeout
- π« Unauthorized user tries to assign custodian
π§© Example: Prevent Non-Owner Ping Without Permission
function testCustodianCannotPingIfNotAllowed() public {
vm.prank(owner);
zaph.setCustodian(keyId, owner, jacob, Role.Writer, false);
vm.prank(jacob);
vm.expectRevert("Not authorized to ping");
zaph.ping(keyId, owner);
}
π§ͺ Donβt forget to run your tests with:
forge test -vv
β‘οΈ Visit the Support FAQ