Testing released courses only0:00
So let's move on to our second test here. It shows only released courses. Here in the range method, we now think about in which state our application needs to be in, in order to make an assertion. So we probably need two courses, one which is released, and one which is not released. I'm going to copy here maybe those tests here. Let's bring them in here as well. And as we said, I only need two of them. And I also don't need the description here and here. Because again, we think about how we're going to test this. Again, we're going to make sure that we see the title on the homepage, but we only want to see it for the course which was already released. So we're probably going to end up here with a released add field. And here I'm going to call now carbon method. And let's say this was released yesterday. And I'm going to import the namespaces. And this one we keep empty.
released add field. And here I'm going to call now carbon method. And let's say this was released yesterday. And I'm going to import the namespaces. And this one we keep empty. Yeah, let's do it like this. So the first course is which we have already released yesterday. And the second we haven't released yet. And then this part is probably the same again. So let's copy this again here. Again, we are combining the act and the assert tasks here. And we want to make sure that we see some text. We want to make sure that we see course A. But we also want to make sure that we don't see some text. Let's also do this for course B. All right, let's run this test and let's see what the output tells us. So SQL error again, courses has no column released. Again, we're going to do here what our failing test is telling us. So I want to create here a new timestamp and we're going to call
Adding released_at column1:51
SQL error again, courses has no column released_at. Again, we're going to do here what our failing test is telling us. So I want to create here a new timestamp and we're going to call it released_at. And also interesting here, I want to make this nullable. So this means when we create a new course, by default, it's nullable, not released. And only if we specifically release it, it should be released. Let's run the test again. What do we see now? We have now a new constraint error that course_description cannot be null. So here we haven't defined the description as nullable. And I don't want the description to be nullable because every course that we enter to the database should have a description. So this means inside our course factory, we have to define this. And by the way, since we're already here, maybe let's also add a title and let's let Faker enter here some random sentence. And then for
Filtering released in controller2:41
means inside our CourseFactory, we have to define this. And by the way, since we're already here, maybe let's also add a title and let's let Faker enter here some random sentence. And then for description, what are we doing? We're using Faker as well. And here we can use a paragraph. So far so good. Let's run our tests again and we can see a different error now. Yeah. So that the output does not contain courseB because we can see courseB is in here. And yeah, that's true because we haven't defined which courses we want to give to the view. So let's go to our controller here. And instead of getting all of them, we want to make sure that the courses that we get back shouldn't have null inside the released_at field. And then we are going to grab all of the courses. And if we run the test again, you can see that this test is passing again. And we only show released courses on the website. And as a little tip here, you might have asked yourself,
And if we run the test again, you can see that this test is passing again. And we only show released courses on the website. And as a little tip here, you might have asked yourself, why are we using a timestamp here for released? Couldn't we just use a Boolean and then just tell if a course is released? Yes or no. And we still could do this, but we have a little advantage here with the timestamp because we still know if a course was released or not. And if we get it back from a database, it is null if it was not released. But additional to that, we now also know when it was released. And this is also something that will be very useful for our next test. Okay. So our test is still passing. We have now three options again. What do we want to do? Do we want to enhance the test? I think this is good already. Do we want to refactor our code now? And mostly I don't want to do this now because I want to first make all the tests.
Testing release date ordering4:35
Do we want to enhance the test? I think this is good already. Do we want to refactor our code now? And mostly I don't want to do this now because I want to first make all the tests pass in the same file, because then it's more clear to see some similarities and you better know how to refactor the code. So this means we have now the third option. We can move on to the next test, which is called, it shows courses by release date. Okay. What do we need in the arrange part? First, I'm going to copy here those two courses because it will be very similar. But now instead of only having one course, which was released, we now have both courses released, but one of them is going to be released now, maybe this one here. And this was released yesterday. Another thing about what do we want to see first? So first I want to see the latest course release. So this means this is course B. And then after that, we should see course A. Let's also copy
Another thing about what do we want to see first? So first I want to see the latest course release. So this means this is course B. And then after that, we should see course A. Let's also copy this part here. Again, we want to act here and assert in the same time. And now instead of just making sure that we see something, we want to see something in a specific order. So first we want to see our course B because this is the one which was just released now. And then we want to see course A. And it's always interesting to think about what will be the outcome if I'm going to run this test, because this will also help you be better at TDD and also predicting what the next step will be. So since we haven't done any ordering yet, this should fail because we should see course A first and then course B. And that's just because that's how they are created. First, we create course A and then course B. And when we just grab them from the database,
course A first and then course B. And that's just because that's how they are created. First, we create courseA and then courseB. And when we just grab them from the database, courseA would be the first one. And yes, it does. We see now courseA first here, and then later courseB. And this is also why it's failing. Okay, so how can we change the order? We need to get back to our controller and also make sure that we order our results by the releasedAt field. And we want to do this descending way. And if we're not going to run the test, you can see it's now passing. So this means now it's in the correct order. We're going to see courseB before courseA. What's also interesting here in this example, let me get rid of this order here again. Sometimes we're going to end up with a falsely positive result. So this means the result of the test is positive, but the code is not working as expected. And this happens
of this order here again. Sometimes we're going to end up with a falsely positive result. So this means the result of the test is positive, but the code is not working as expected. And this happens now if I'm going to change this now. So let's say the first Course was created now and the second was created yesterday. So this means we want to see now Course A first because that's the one which was released more recently. And I have commented out the way where we're going to order this. And if we now run this test, it should pass here. And it does. And that's just because of the order of how the Courses are being returned from the database. Since Course A is the first one which we created, it's also the first one which we get back. So this is already the default order that we see Course A before Course B. And that's why it's always helpful to switch the order around already in your tests by first creating the one which we don't want to see first, and the second.
Preventing false positive tests8:12
that we see CourseA before CourseB. And that's why it's always helpful to switch the order around already in your tests by first creating the one which we don't want to see first, and the second one is which we want to see first. So let's change this again. Now it should fail again. Yeah, it does. And now let's bring in our solution here. And we should be back to good here. Yes, we are. Okay, next now we have made all of our tests pass. Let's run them now all together. I can do this by clicking in between two of the tests and running my run context configuration shortcut. And yeah, okay, we can see now that the first test is failing now. So let's check this again. And we can see that we don't get anything back from our request. And yeah, that's because inside our second test, we have introduced this released field. And now all the courses which we have created are not released inside our first test. So this means now we also need to release all of those
test, we have introduced this released field. And now all the courses which we have created are not released inside our first test. So this means now we also need to release all of those three courses. So field is called released at and we're going to want to release it. Let's say just now we don't care when it was released, because it just should be released. Okay, this looks good. It's already getting a pretty messy here. But we're going to deal with this later. Let's run this test. Now this test is working. Let's run all of our tests. And they are now working. So it's always a scenario, we're going to change some code, the next test is passing, but you change something for the first test. And then you have to adapt this. But now at least we know that our tests will tell us if we did something wrong, because if we didn't have tests, then we might didn't see this, which would be very painfully. Alright, and then the last thing
Fixing failing suite tests9:59
know that our tests will tell us if we did something wrong, because if we didn't have tests, then we might didn't see this, which would be very painfully. Alright, and then the last thing to do every time your tests are passing is also check that all of the tests are passing. So also autofiles. And what I like to do here is used to run anything method in PHPStorm. And here we're going to run the php artisan test command, which you can use in the common console as well. Let's run this. And oh, we can see that of our five tests, one is failing. So let's check this out. It's the pagesResponseTest. Let's see why this is failing. We have an SQL error, because there is no such table courses. So what's happening here is that we now have introduced courses to our homepage. But now we don't have any courses here defined. And we also are not refreshing the database. So at this point, we actually don't have any database at all.
courses to our homepage. But now we don't have any courses here defined. And we also are not refreshing the database. So at this point, we actually don't have any database at all. So let's change this by using pastUsers method here. And again, we're going to use the refresh database straight and let me import the namespace as well. Okay, let's run this again. This looks better now. So we don't have any courses in the database. But we don't test this here. And this is why we don't have to create any courses for this test. The code is still working. Because if there are no courses, which just don't show anything on the homepage, which is something with I am totally fine. So one last time, let's run all of our tests here. And now all should be clean. Yeah, we have now five passing tests. And this also means inside our to do list, we can check off our first to do the guests can see the courses overview.
And now all should be clean. Yeah, we have now five passing tests. And this also means inside our toDoList, we can check off our first toDo the guests can see the courses overview.
