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.

Episode summary

In this episode, we quickly dive into the concept of higher order tests and how they can drastically simplify your test code. We take one of our existing tests and show how it can be refactored into a much more concise form using higher order chaining, which lets you remove the callback functions and just chain your testing methods directly. This not only makes your tests easier to read but also saves you a ton of time in the long run.

We compare the "old way" of writing tests with closures and the "higher order" way—basically chaining methods like post, assertSessionHasErrors, or get and assertStatus all in one line. You’ll see how super simple assertions (like checking a route returns a 200 status) can be written out in a single statement, instead of writing out a full test class and methods. It's a real game changer for basic tests!

We also touch on a small gotcha when chaining on higher order tests and hint that there’s a bit more to be aware of for things like assertDatabaseHas. That’s coming up next, so stay tuned!

Episode discussion

No comments, yet. Be the first!