In this episode, we take a closer look at higher order assertions in Pest. Remember how we've used higher order tests already to make things cleaner and more readable? Well, Pest gives us a similar pattern for our assertions, letting us chain them together neatly instead of repeating ourselves.
We start by looking at a typical assertion you'd write in PHPUnit or Laravel's testing framework—where you'd have to break things apart and check each condition separately (like checking the count, then checking if a collection contains a value). Then, we rewrite these using higher order expectations in Pest. This means we can use expect()
once and chain as many assertions as we want, such as toHaveCount(1)
and then pluck('id')->toContain($friendId)
. Not only does this make our code cleaner, but it's also way easier to read.
You'll also see some handy extras, like checking if something is an instance of a Collection, all in the same neat chain. Plus, by the end, you'll see how higher order expectations help you avoid repeating expect()
calls and keep your tests tidy. Feel free to check out the sample code in the course for reference!