Playing
06. Switching to higher order tests

Transcript

00:00
So this is going to be a very quick episode but it's going to save you a huge amount of time writing tests in the future and we're going to talk about higher order tests. We're going to focus on this test for now and switch this over and then we're going to see
00:14
the issue that we'll come across with higher order tests when we do something like this where we have to then call something like assert database has. So let's focus on this for now and then we'll cover that other issue in the next episode. Okay so a higher order test looks like this.
00:30
Let's just go and copy this down to here so we can compare the two. So a higher order test is going to look like this. We define out the it or test function, it doesn't matter what you use, but instead of providing in a callback function closure in here, what we can do is directly
00:50
chain on any of the methods that we would normally call inside of here. So I'm just going to directly chain on post through to register and then down here I can directly chain on assert session has errors and we can do exactly the same thing.
01:07
So we can just grab the errors that we were checking for. So that is the difference in not having to define this callback in here and instead directly chain it onto here in this higher order way. So let's go ahead and just get rid of this test now because we've refactored that test down
01:25
and let's go and run our register test again. And of course if we actually end that off with a semicolon that is now going to work. So it works in exactly the same way we have just shortened this down and if you had something really really simple,
01:40
for example if you wrote a test to just hit the register page and make sure that you got a 200 status back, we could just say it shows the register page and all you need to then do is say get slash register and then assert status 200. So you've just written something that would normally require a huge class
02:04
with methods in one line just inside of here. So again let's just go ahead and rerun register test and we do get a failure here because we have this as auth register. So there we go.
02:17
We've got three tests now passing here. Okay so we're going to talk about this in the next episode because just converting this over to a higher order test isn't quite going to work and we'll see why soon.
35 episodes4 hrs 19 mins

Overview

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!

Comments

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