1/5
_Follow along with the video_ --- ### Introduction In this lesson we are going to build a couple more tests. If we check our code coverage with `forge coverage`, the terminal will show that we are only at around 53% coverage for the `Raffle.sol` contract. Code coverage refers to the percentage of lines of code that have been tested. > š” **TIP**:br > Achieving 100% coverage isn't always required, but it is a recommended target. ### `checkUpkeep` tests To improve our coverage, we need to write additional tests. For example we can address the `checkUpkeep` function, to ensure it really executes as intended under various circumstances. 1. Letās start by ensuring that `checkUpkeep` returns `false` when there is no balance. Weāll do this by setting up our test environment similarly to previous tests but without entering the raffle. Hereās the code: ```solidity function testCheckUpkeepReturnsFalseIfItHasNoBalance() public { // Arrange vm.warp(block.timestamp + automationUpdateInterval + 1); vm.roll(block.number + 1); // Act (bool upkeepNeeded,) = raffle.checkUpkeep(""); // Assert assert(!upkeepNeeded); } ``` 2. Next, we want to assert that `checkUpkeep` returns `false` when the raffle is in a _not open_ state. To do this, we can use a setup similar to our previous test: ```solidity function testCheckUpkeepReturnsFalseIfRaffleIsntOpen() public { // Arrange vm.prank(PLAYER); raffle.enterRaffle{value: raffleEntranceFee}(); vm.warp(block.timestamp + automationUpdateInterval + 1); vm.roll(block.number + 1); raffle.performUpkeep(""); Raffle.RaffleState raffleState = raffle.getRaffleState(); // Act (bool upkeepNeeded,) = raffle.checkUpkeep(""); // Assert assert(raffleState == Raffle.RaffleState.CALCULATING); assert(upkeepNeeded == false); } ``` ### Conclusion By writing these additional tests, we enhance our test coverage rate, improve the reliability of our `Raffle.sol` contract, and check that `checkUpkeep` behaves correctly under various conditions.
A comprehensive guide to testing a Solidity smart contract with Foundry. This lesson covers the basics of creating a testing environment, writing unit tests, and understanding code coverage.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
Foundryup, Foundry Forge, and Anvil
Blockchain Oracles
How to create local Blockchain testnets
How to verify a smart contract
How to write and run smart contract tests
Security researcher
$49,999 - $120,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Guest lecturers:
Last updated on November 29, 2024
Solidity Developer
Foundry FundamentalsDuration: 2h 52min
Duration: 2h 56min
Duration: 26min
Duration: 5h 22min
Course Overview
About the course
Foundryup, Foundry Forge, and Anvil
Blockchain Oracles
How to create local Blockchain testnets
How to verify a smart contract
How to write and run smart contract tests
Security researcher
$49,999 - $120,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Guest lecturers:
Last updated on November 29, 2024
Testimonials
Read what our students have to say about this course.
Chainlink
Chainlink
Gustavo Gonzalez
Solutions Engineer at OpenZeppelin
Francesco Andreoli
Lead Devrel at Metamask
Albert Hu
DeForm Founding Engineer
Radek
Senior Developer Advocate at Ceramic
Boidushya
WalletConnect
Idris
Developer Relations Engineer at Axelar