07. Tapping with higher order tests


Okay, we're going to attempt to move this over to a higher order test and see why it just won't work if we try and chain all of these methods onto each other. So once again, let's just copy this down just so we can reference the original test here
and let's just start to change this over. So we can get rid of everything inside of it and let's get rid of our callback here and then let's go down and just continue. So we want to post through to register with these details.
So let's do that now. Post to register and we'll paste that array in. Now that will work on its own. So if we just come over and rerun our register test, we can see, yeah, that already exists.
So let's just say higher order for this one. Let's run that and it works. Although we have an incompleted test because we're not making any assertions. Now if we come up, what we can also do is assert that we're redirected back to the home page.
So we can come down and continue to chain this on and we can run this and you can see that sure enough it passes. But as soon as we try and do something like assert database has, for example, on here, let's say users,
and let's just keep this simple and just look for the email address rather than the name as well. We'll see that when we run this test, it's not quite going to work. Now the reason that this doesn't work is we have an undefined assert database has method being called on our redirect response.
Now what's happening here is when we run post and assert redirect, specifically post, that's going to return to us the test response class, which we can then chain on assert redirect. But if we look at how we did this up here, we called this assert database has.
We didn't call this on the response itself. So just to demo this out, if that doesn't make sense, let's go ahead and assign a response variable to here and let's get assert redirect removed and just die dump on this response.
And we can even say get class response and go ahead and run our tests. And you can see that we get a test response back here. Let's open that test response up and let's take a look at what we can do inside of here. So let's go down to assert OK, assert created, assert no content.
So it's got all of these assertions available on this, but it doesn't have assert database has. This is a completely separate method being called to just do a completely separate check. This is nothing to do with the response itself.
So basically, we can't just chain this in in a higher order test like this. So how do we get around this? If we want to write more elegant tests with pest, what we get around this by tapping the original result of the it function,
perform this post, perform any other assertions we need on the response itself, and then continue to chain. So this is what this is going to look like. Again, let's just start this from scratch and we'll rebuild this up.
So it looks a bit nicer. So down here, what we're going to do is use Laravel's tap helper and we'll create an inline or a shorthand function in here to deal with this. In fact, we don't even need to do that.
Let's create a full one and then we'll refactor this down. So we're going to tap into this. We're then going to perform the kind of response stuff that we need to do, like posting through to register with them details.
So let's again just copy all of the details that we need in here. So let's paste this down and then go ahead and assert redirect slash. But now we've tapped into this. What we can do is continue outside of this tap to chain anything onto it.
So tap is going to go ahead and perform these, but return this to us. So then we can continue down here to assert database has users. And again, we'll just put that email address in there just to keep things simple. So Mabel at co-course dot com.
And what else do we have? Assert authenticated. So we could then chain that on down here as well. So what we basically do is perform this response stuff,
but then return the original result of this it function. So we can continue to then chain on in that higher order way. So I'm going to get rid of this original test up here. So let's get rid of all of that.
Let's change the name of that back as well. And we now have a much more clean way of defining this rather than having that closure in here. Let's go ahead and rerun our register test.
And you can see sure enough now this passes. Now, if you want to make use of PHP's shorthand functions, you can do that as well. So we could take everything that we're doing in here
and we can instead include a shorthand function and then just go ahead and paste that in. And you could pull that down and kind of configure this how you wanted to.
But I tend to just leave them like this in a bulk. So there we go. We've shortened this test down. That's still going to pass.
We've just done that by tapping into this, returning the original result of this. So we can continue to effectively do what we were doing before with this.
And again, if you had a much shorter test like this and you're just sending a really simple request down and then checking something, you're going to end up with a really short set of 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.