تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Refactoring test setup0:00

As always, when we've finished our feature and all the tests are passing like they do, it's the best time to start refactoring our code. And of course, there are already a few things that I'd like to improve from the code from our last feature. Again, I want to start here by getting rid of most of those static text lines here and here, which we can do better. Okay, first, let's see again how we can get rid of all the text here. And I'm just starting by getting rid of all of that. Okay, again, similar to the last time, our tests are now failing because the course tagline, the things that we define here are now not seen anymore. So we're going to bring in here the tagline, whatever this tagline is. And for the learnings, we can use the spread operator because this is an array, and now we're spreading out all the items of the array, which we get from our factory. Let's run this test again, and you can see that this is passing,

Asserting full image path0:53

spread operator because this is an array, and now we're spreading out all the items of the array, which we get from our factory. Let's run this test again, and you can see that this is passing, and this already looks way cleaner than before. Then something else that I noticed that we're making sure that we see only the image here. So let's check our course details here. Yeah, we're only passing in here the image. But actually, what I want to do is I want to see the full path. Something like this, which is more realistic. When I'm going to run this, this will fail now because, yeah, we don't see this full path here. And here we're going to provide now the asset helper method as well, which gives us a path through our public folder, images, and then the course image. And yeah, this is working as well. And yeah, here in our test, we are now a little bit more specific about what we want to see,

Renaming to imageName1:42

through our public folder, images, and then the course image. And yeah, this is working as well. And yeah, here in our test, we are now a little bit more specific about what we want to see, which is not only the image name but the full path here. Since we're already talking about the image here, what I don't like about the name which we have chosen is that it doesn't tell us anything about is it a full image? Is it the path? What is it? So I like to be more specific here as well to make this more readable and so that it's easier for other developers to see what we actually have here. So let's think about what would be a better name since we are only providing the name of the image without any path. I think it's a good idea to just call it imageName. Of course, this should not fail. No, it doesn't. And it does not fail because yet this imageName is not given. That's why we're only checking if we see a specific URL with images and then nothing behind it. So again,

No, it doesn't. And it does not fail because yet this image name is not given. That's why we're only checking if we see a specific URL with images and then nothing behind it. So again, it would be a good idea to first define the image name ourselves like image name. And now our test will tell us that there is no column named image name. So that's also why I always want to start with static strings when I start testing new fields. So let's change the name here like this. Let's run the test again. What's now? Table has no column named image. That's true because we have changed this. So this means we are now getting this error because of our factory. So let's change this to image name. And now we have a different error about the output because we only see a string without the image name, but we're expecting the image name. And this is now because here we also have to change

And now we have a different error about the output because we only see a string without the imageName, but we're expecting the imageName. And this is now because here we also have to change the field to imageName. Let's run this again. Yeah, this is working. Maybe there's some other place where we have used this, but for now this looks good. So this means now that this is working, I can get rid of this again. We don't need this anymore and it should still work. Yes, it does. And I think it's now better that we're using a more descriptive name, like imageName. And let's now also try to clean up this a little bit more. So here we are defining the relationship through the courseId, which we define ourselves here, but there is also a better way we can do this. So while we are creating our Course, we can also say that the Course has a relationship and the relationship is to the Videos model. And here

Using relationship factories4:24

but there is also a better way we can do this. So while we are creating our Course, we can also say that the Course has a relationship and the relationship is to the Videos model. And here we're providing a new factory and we can also add account here, but we could also change the data, which we don't need here, but this is very handy. So this means I can get rid now of this whole line. The test is still passing and now take a look how good this is to read. We are creating a Course which has Videos, three of them, and we're going to create them. I really love using those factory helper methods. And then another thing that we didn't think about is here we are creating a Course as well as in the first test and both Courses are not released yet. So do we want to show Course details for a Course which is not released yet? I don't think so. So probably it's a good idea to add another test here at the top to make sure that we only see Courses which are released.

Restricting unreleased courses5:13

show course details for a course which is not released yet? I don't think so. So probably it's a good idea to add another test here at the top to make sure that we only see courses which are released. Let's copy this code from the first test and what we now want to make sure is that when we have a Course which is not released and we make a request to this Course that we see a different output here, we can use the assertNotFound method so that we get back a 404 HTTP status code. Let's run this. We should know that this should fail because we have a 200 code, because we haven't implemented this feature yet. So let's add this to our controller. In our controller let's check if our course is released and if it is not released we want to throw a new NotFoundException so similar to what Laravel does itself if it doesn't find anything. And of course our released field is called released_at. Let's test now our code again.

want to throw a new NotFoundException so similar to what Laravel does itself if it doesn't find anything. And of course our released field is called released_at. Let's test now our code again and yeah this is now working perfect. But this now also means that probably some of our other tests are now not working anymore. Yeah here we go. So this means for every Course that we create here we want it to be released. This test should now pass again it does and let's also do this here as well. Perfect. So this means now we have made sure that we don't see any details of a Course which was not released yet.

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