در حال بارگذاری ...

Laravel 5.1 Testing Overview0:00

In Laravel 5.1, the testing facilities have been massively improved. Let's take a look at example test. Alright, well now, look how natural this is. And in fact, if this looks familiar to you, and if you went through the Intuitive Integrating Testing series at Laracasts, well, it looks so familiar because most of the integrated API has now been integrated into Laravel 5.1. That means now, there's literally nothing you need to do to write tests that are this readable. Let's try it out. But first, real quick, if we go to composer.json, take note of some of your development dependencies. These are third-party libraries that you now have access to. Faker. This allows you to essentially generate dummy data.

These are third-party libraries that you now have access to. Faker. This allows you to essentially generate dummy data. We've covered it at Laracasts, and we'll talk about it a bit more in the next video. But you also have access to Mockery for mocking classes. And then, of course, you still have phpUnit and also phpSpec. For plenty of projects, you will make use of all four of these. Alright, so let's run phpUnit. And cool, we get green. So, let's see how this works. Well, we are visiting the homepage, and we expect to see Laravel 5.

Adding Gulp TDD Runner1:06

So, let's see how this works. Well, we are visiting the homepage, and we expect to see Laravel 5. Well, in routes.php, we have a fresh install of Laravel 5.1. So, the homepage loads the welcome view, and the welcome view, if we scroll down, sure enough, displays Laravel 5. Why don't we do this? Really quick, before we talk about this a bit more, in my gulpfile.js, I'm going to mix phpunit. Now, I can run gulp tdd, and now, whenever I change a test or a file within the app directory,

Now, I can run gulp tdd, and now, whenever I change a test or a file within the app directory, it will rerun phpunit, essentially. So, here's what I mean. Let's go to welcome.blade.php, and we're going to change this to hello world. And now, back in ExampleTest, well, this should fail, right? Let's try it. I'll save it, and there you go. We get red. And specifically, if I switch back to the terminal,

Form Testing Teaser1:57

We get red. And specifically, if I switch back to the terminal, well, the returned response, we couldn't find Laravel 5 anywhere. Cool. So, what else can we do here? The API is actually pretty extensive, and I'll let you review the documentation. But for a quick teaser, you can even do basic stuff like filling out forms. For example, what if you wanted to test some kind of search feature?

you can even do basic stuff like filling out forms. For example, what if you wanted to test some kind of search feature? When I visit some page, and I type some query into a text box that has an ID of search, and then when I press the search button, well, I expect to see search results for some query on what page? On the page, how about search results? That's a perfectly good and valid test. Okay, let's make it pass. Well, to begin, we don't have any kind of form.

Building Search Form2:47

Okay, let's make it pass. Well, to begin, we don't have any kind of form. So, back to welcome, let's get rid of all of this, and we'll say form, and then we need an input with an ID of search, and we'll set a name of search as well. Next, we need that button with a type of submit, and its value will be search. Finally, don't forget that Laravel wants us to add a token here to protect us. So, we could say input with a name of token,

Finally, don't forget that Laravel wants us to add a token here to protect us. So, we could say input with a name of token, and then we could set the value to CSRF token. And yeah, that should do it. So, let's see if we've satisfied this test. Well, we visited the home page, we do have a search text box, so we will type into that. We do have a search button, so we submit that, but then what about this section?

We do have a search button, so we submit that, but then what about this section? Well, why don't we have the method be post, and the action will be, like we said, searchResults, or whatever. Now, within routes.php, when we make a POST request to searchResults, well, we're sort of going to cheat and just return whatever we had here. That. return, and you know what? Why don't we run that through as printf.

Return, and you know what? Why don't we run that through as printf. So, we can replace that with a string, and then bind that to request, input, search. Okay, I'm going to save that, and let's see where we are. Sweet, we're at green. So, what I want you to notice is just how incredibly intuitive all of this is. You write it out basically the way you would speak it. When I visit the home page, and I type this text, and then I hit the search button,

Using Test Traits4:45

Any of these represent functionality that you can import into your test. All you need to do is say use, and then the name of the trait. So, in this case, if I import the trait, well, we are saying for these tests within this class, I don't want any middleware whatsoever. That can be useful in some situations. Or database migrations. Well, let's see what happens here. Before the test runs, that's a PHPUnit annotation that just says after the setup method, directly after that,

Resetting Database State5:11

Before the test runs, that's a PHPUnit annotation that just says after the setUp method, directly after that, call any methods that have this annotation of before. And in this case, we will run php artisan migrate. And then, well, this is just hooking into tearDown. And at that point in the life cycle, we will roll back our migration. So, it's just a fairly slow way to reset your database, so to speak. And that can have its uses. But, generally, I would recommend that you use database transactions. Now, if we take a look at that one,

But, generally, I would recommend that you use database transactions. Now, if we take a look at that one, well, on setup, we begin a transaction. And on Teardown, we roll it back. So, basically, with this method, we never fully commit or persist the data to the table. We never get to that point. We just sort of roll it back or reset it. And that method is actually very, very fast. So, that means whenever you want to set up some test data,

have access to a set of data that old tests did not. You always want to make sure that the world is identical for every single test. So, if you simply import this trait, we'll take care of the rest. Okay, so as cool as this is, there's more to the testing facilities in Laravel 5.1. We'll talk about that in the next video.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟