27. Higher order assertions


Okay, so before we move on to our feature tests, let's have a look at pests higher order Expectations now we've already come across higher order tests where we can do something like this it
does something and we can just chain on things like posting through and Then going ahead and asserting that things were okay That's a great way to write tests, but we can do the same thing without expectations Let's have a look at how this works
By just coming up to a test like this and see what we can do to make this a little bit clearer Traditionally within PHP unit or Laravel's testing framework. We would have to do these separately So we'd have to expect the accepted friends of mine to have a count of one and the same for this as well We'd have to pluck this out and then check that it contains that friend. Let's rewrite this to use a higher order
Expectation so we're gonna go ahead and still use expect We're gonna pass in the thing that we're checking But remember we're doing multiple checks or multiple expectations and assertions on this. So we're gonna say to have count One that's pretty much exactly what we wrote here. So there's no difference here
The only difference is I've pulled this down to a new line. But now what we're gonna do is say pluck ID and then we're gonna say to contain friend ID So we could pull that up a little bit just because it makes a little bit more sense to have this in line because this
plucking relates to this particular to contain Expectation, but that's pretty much what we can do We can now get rid of these two things here and that tends to read a little bit better It's just rerun our test and sure enough it still passes and this is completely valid now if we just scroll down we don't
Really have anything that uses the same thing. Technically we have to have the same thing passing to expect So if for example, we were doing something else on user friends, we could just pass that in for now, though We don't have anything that uses the same data that we want to run these expectations on that's absolutely fine But we could do for example
To be instance of Collection if we wanted to so let's go ahead and make sure we pull the Laravel collection in and There we go. That works if you wanted to do something like that, but basically with higher-order expectations
We can just continue to chain on what we want to do instead of having to redefine Expect every single time I'll leave this in here just for reference in the downloadable source code But hopefully that makes sense now. So when you are writing things like this Just chain them on much like we saw with higher-order tests
35 episodes4 hrs 19 mins


Pest is a PHP testing framework that brings beautifully simple syntax to your tests, without sacrificing on features. In this course, we'll get up and running with Pest in a Laravel project and write tests for a real-world application that we'll build along the way.

You'll learn how to set Pest up in a Laravel project, write tests with Pest's built-in assertions, generate code coverage, and more.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!


No comments, yet. Be the first to leave a comment.