Playing
23. Asserting only specific validation errors

Transcript

00:00
In Laravel 12 we have a new assertion for validation which can help prevent false positives and really just clear up our tests.
00:08
So let's take a look at this now. Now I've created a really simple register controller here. It doesn't actually do anything. It doesn't even pass in a password. It just accepts a name and an email address with some very very basic validation rules just so we
00:23
have something we can test with. Okay let's go ahead and create out a test for this. So of course I'm going to call this register test and let's head over and write a test for just checking the email validation pretty much in the way that we would always do.
00:38
So let's say it fails if an invalid email whatever we wanted to say and we'll go ahead and post through to register. We'll pass some data in so let's get rid of this assert status. We will pass in a name and we're going to make that valid
00:55
for now. We'll see why this new method is really helpful in a minute and of course for the email we're going to make that bad. What would we normally do in this instance if we were testing this email in isolation? Well we would use something like assert
01:09
invalid on that email and let's spell assert correctly. Okay so let's go ahead and run this so let's run test features and register test and yeah sure enough this passes. Now the problem that we have with assert
01:24
invalid is that sometimes we can get a false positive if the overall test fails. Now remember this is testing if we have an invalid email address. We might be testing the other way around to make sure that we can get through with the validation.
01:37
Let's go ahead and mess this up in our test and see it still pass. So basically we're going to take something else in here that could fail. Let's put a minimum requirement on this of a hundred and just be really silly here. Let's go ahead and run our test again
01:53
and you can see it passes. Now technically that test is okay but it's failed for another reason. Now this is the best example I could come up with but I'm sure when you are actually writing a more complex test here with more data
02:09
or you're looking at a successful registration response this could fail for another reason. So the way that we get around this now is we have a new method to assert here and we're going to say assert only invalid on email and we can pass as many fields here as we want. So
02:26
if we were testing multiple invalid fields in one go we could do that as well. So just by changing this over what is now going to happen is this test is going to fail because it has failed for another reason. So we have the
02:42
name field must be at least 100 characters here and also this has failed for that reason as well. But what we're checking is that in the specific test case that we've created here only the email is invalid.
02:57
So you can now use assert only invalid when it makes sense for your test case to prevent false positives like this. So I'm going to go ahead and just fix up the minimum field here and we get a passing test.
26 episodes2 hrs 34 mins

Overview

Need to know what’s new in Laravel as it happens? Every episode of this course is dedicated to covering the most interesting and useful Laravel additions in detail, so you’re ready to start using them in your applications.

Check back often, and stay completely up-to-date with Laravel.

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

Episode discussion

No comments, yet. Be the first!