Testing Course Videos Count0:00
So again, you want to make sure that we see a specific number of videos which belong to our Course. So let's create a new Course, but what we also need here are some Videos. So let's imagine we have a Video model with a VideoFactory, and we want to create now three Videos, and they should belong to the Course. And then we're going to make a GET request to our new route courseDetails. We provide our Course as the argument, and then we want to make sure that we see some text on the page, for example, three Videos, just to make sure that three Videos belong to our Course. Okay.
Fixing Factory Constraints0:47
to our course. Okay. Here we go. Let's run the tests. What do we see? Not null constraint for course's tagline. So yeah, we have now introduced a new tagline, but we are not specifying the tagline here, so we could do this, but this is actually what our factory is for. So let's create a new tagline, this faker sentence. All right.
So let's create a new tagline, this faker sentence. All right. What else? Not null constraint failed for image, so yeah, we have the same for the image. And here I'm just going to use a static string for now. What's next? We have no default learnings. Okay. So let's do this as well. So we add here learnings, learnA, learnB, and learnC.
Creating Video Model Factory1:31
So let's do this as well. So we add here learnings, learn A, learn B, and learn C. Okay. What about the test now? Okay. The class Video is not given. Okay. We have a new error and a new instruction. Let's create now a new Video model. Let's call it video.
Let's create now a new Video model. Let's call it video. And we also have here now a new recreation, but I'm not adding anything here yet. Let's check again. Okay. Video is still not given because we need to import it. Here we go. What next? Call to undefined method factory. Okay.
Call to undefined method factory. Okay. We have to create now a new VideoFactory. Here we see it already. Let's go. Let's run it again. Call to undefined method factory again. That's now because, yeah, we haven't added the factory to our Video. So here you need to define that we want to use the HasFactory trait, just to tell thereabout that we have a factory for this model.
So here you need to define that we want to use the HasFactory trait, just to tell thereabout that we have a factory for this model. So what's now? Table videos has no column name course_id. All right. Let's go to our migration. And this here, we're going to use here an unsignedBigInteger for the field course_id. So every Video belongs to a specific Course. What's next?
Displaying Video Count View3:03
So every Video belongs to a specific Course. What's next? Okay. Finally, we see an output issue. So in the output, which we get, we don't see that we have three videos yet. So in order to see how many videos we have, we need to go to our course details view file. So let's add here another paragraph where we're going to grab all the videos from our course. And let's also get a count method around this so that we get the count of the videos and then videos as a string.
And let's also get a count method around this so that we get the count of the videos and then videos as a string. So I think we should finally now be green again. Let's try this out. Yes, we are. Just to be sure, let's also run both tests here. And we can see now the first one is failing. Okay. Let's check the error. Type error, count argument one value must be of type countable.
Ensuring Successful Test Responses3:54
Let's check the error. Type error, count argument one value must be of type countable. Now give. Okay. So this is now interesting because here on our view file, we get here now back, but actually this is not only the case for the first test here. It's also the case for the second test here because we haven't defined a relation from courses to videos yet, but still when we run this, this will give us a positive response. And let me show you what this is about. There is a method in Lua which we can use to disable the exception handling.
And let me show you what this is about. There is a method in Lua which we can use to disable the exception handling. And if we do this, we see now the same error here again. So this test is passing, but actually there is an error from, so if we add an assertion to make sure that we get a positive response back from this test. Now we also don't need this here. We should see an error. Yeah. Now we see this error. So that's why it's always important to make an assertion that you get a successful response.
Now we see this error. So that's why it's always important to make an assertion that you get a successful response back because if not, this assertion is passing because this whole code here is inside the error output if you debug locally. So this means we still see this text here, but only because this code is included in the error response. So this is very important to understand. So please always make sure to use an assertion when you make a GET request or POST request to a page that you get a successful response back and only then add an assertion. Okay.
Testing and Adding Relationships5:24
to a page that you get a successful response back and only then add an assertion. Okay. So this means this leaves us here with an error as well because yeah, we haven't defined the relationship yet. And the relationship belongs to a model. That's why I'd like to add a separate test to our Model test. So inside our CourseTest, let's create now a new test to make sure that the relationship for videos is defined. Let's call it, it has videos. So we're going to need a Course here.
Let's call it, it has videos. So we're going to need a Course here. And then also for this Course, we need some videos. So VideoFactory, create courses with the course_id of $id. And yeah, let's also make three of them $count three, yeah, like this. And then here we are going to act and assert again, and we can make use here of the expectation API of Assert. So we expect that when we call the videos relationship on our Course, so this should give us back a collection. So we can run the count method on it to make sure that there are three items.
give us back a collection. So we can run the count method on it to make sure that there are three items inside our collection. And then what I also can do is I can now run the each method on the collection. And I want each instance to be an instance of the Video class. So this is a very simple way to test that a relationship is defined for one of your models. So let's run this and we see an error that we get something wrong for the count method. So we're trying to count something, but what we get back when we count the videos property relationship here is now.
So we're trying to count something, but what we get back when we count the videos property relationship here is now. Okay. So now it's the time where we're going to add the relationship to our Course model. So let's add here a new relationship, public function, we're calling it videos, and we're going to return a hasMany object. So let's do this. This hasMany videos. So we're providing the Video class here to define the relationship from our Course to videos.
So we're providing the Video class here to define the relationship from our Course to Videos. All right, let's run this test again. It has Videos, this one here, and it is now passing, which is great. So this means we can get back to our courseDetailsTest and let's run this. And this is now also working as well as both of them here in this file. And just to be sure, let's also run all of our tests and yeah, we are still good. And we already have now 10 tests for our application. All right. So far so good.
All right. So far so good. Let's take a look at our to-dos list and we can check now of the second task. Of course, we haven't refactored the new code yet, but we're going to do this in the next video.
