Defining Feature Testing0:00
All right, let's move on to a different style of testing that unfortunately might go by many names dependent upon the development community. Some refer to it as feature testing, you might hear the term end-to-end testing, you might hear acceptance testing, you might even hear functional testing, you might hear, what am I missing, feature testing, browser testing, and even some communities distinguish slightly between these. For example, your feature test might be passing, but the acceptance test is failing because it doesn't meet the customer's expected criteria. It gets confusing very quickly. So generally, I envelope this whole idea under a standard term of feature testing. And a feature test tests a feature in your system. So while a unit test is very isolated and very targeted, when I call this method, I expect that response, right? When I call 2 + 2, I expect 4 to be returned. That's what I mean by very targeted. A feature
Introducing Breeze Scaffolding0:47
is very isolated and very targeted, when I call this method, I expect that response, right? When I call 2 plus 2, I expect 4 to be returned. That's what I mean by very targeted. A feature test, on the other hand, is sort of a zoomed out perspective. So when I, for example, visit a form and I request a password reset, as a side effect, I expect an email to be delivered to me. And when I click on that email, I should then have the option of resetting my password, right? It's very zoomed out versus a unit test. So you'll see what I've done here is I've installed Laravel Breeze, which, if you're unfamiliar, is a first-party authentication scaffolding. And you'll see it gives me some initial migrations, some views, some controllers to get the typical authentication layer out of the way. Now, as part of installing Breeze, they will give you a handful of feature tests. So let's just look at RegistrationTest here. Again, notice this is very zoomed out.
Walking Through RegistrationTest1:38
out of the way. Now, as part of installing Breeze, they will give you a handful of feature tests. So let's just look at RegistrationTest here. Again, notice this is very zoomed out. When I make a POST request to this endpoint, so we're starting with a POST request rather than maybe instantiating a class and then calling a method, here we're just saying, okay, well, if I hit this register endpoint and I give it this POST data, some kind of magic should happen, and then the User is now authenticated and they should be redirected to, in this case, their dashboard. Okay, so if we were to give this a run, it works, as you see there. But yeah, to show you, if I were to go to that controller, so here's where we register a User. You can see, again, this is provided by Laravel Breeze. Validate the request, create a User, fire an event, log in the User, and redirect home. Let's just comment out this section here. And if I run that
Testing Unique Email Validation2:27
this is provided by Laravel Breeze. Validate the request, create a User, fire an event, log in the User, and redirect home. Let's just comment out this section here. And if I run that test again, of course it's going to fail because the User is not authenticated. So again, this is a very typical example of a feature test. Now if I switch back, yeah, you could even test other things. For example, what about attempting to register a User with incorrect fields? Maybe if you register with an email address that already exists in our system, well, that shouldn't work, right? So we could try this out real quick, and we could say test registration requires a unique email. So you could use this for testing your validation. Here's how we might do that. Let's go ahead and seed our users table with an existing User, maybe with an email of user@example.com. Okay, so now we already have a User in our system, and let's import that
Let's go ahead and seed our users table with an existing User, maybe with an email of user@example.com. Okay, so now we already have a User in our system, and let's import that with this email. So now if we try to register a new User with that email, this shouldn't work. So if I give it a run, I'm pretty sure it's going to fail. Yeah, notice the User is not authenticated. Instead, I could do something like response, assert, let's see, session has errors, that would be an option. Or actually, I don't think it's yet available, assertValid. But if you're watching this, it's very possible that you'll have an assertValid method or assertInvalid. It's just not yet at the time of this recording in the latest version of Laravel, but it will be. Anyways, until then, I can stick with assertSessionHasErrors. So if I were to comment this section out, we give it a run, and now it passes, because validation has failed. If we did not have
Wrapping Up and Next Steps4:06
Anyways, until then, I can stick with assertSessionHasErrors. So if I were to comment this section out, we give it a run, and now it passes, because validation has failed. If we did not have an existing User, of course, it won't pass, because we expected to find validation errors in the session, but of course there weren't. So you could even say, assertResponseHasErrors, and assert, what is it, not, assert, there it is, assertGuest and not authenticated. So we give that a run, and it passes. And in fact, we could even inline all of that just a little bit, and it works. Okay, so you get the idea. A feature test is a zoomed out style of test that ensures that a particular feature works. In this case, that feature is user registration. Now, I did note that some communities distinguish between different styles of zoomed out testing. So with that in mind, in the next episode, I want to talk about browser testing, which again, I would still
