Ok, so we've created all the functions, but did you think I will leave you without automated testing? We need to make sure that our API is working now, and also will not break with future changes.
Our goal is to cover all endpoints with tests, some of them with success/failure scenarios.
Notice: if you haven't written any tests before, you can also watch my full 2-hour course Laravel Testing for Beginners.
First, we need to prepare the testing database for our tests. For this simple example, I will use SQLite in-memory database, so in the phpunit.xml that comes by default with Laravel, we need to just un-comment what's already there: the variables of DB_CONNECTION and DB_DATABASE.
phpunit.xml
<php> <env name="APP_ENV" value="testing"/> <env name="BCRYPT_ROUNDS" value="4"/> <env name="CACHE_DRIVER" value="array"/> <env name="DB_CONNECTION" value="sqlite"/> <env name="DB_DATABASE" value=":memory:"/>
Now, whatever DB operations will be executed in our tests, they will not touch our main database, but rather will execute in memory, in a temporary database.
Now, let's start writing tests, in roughly the same order as we created this app - from the authentication layer. We create a feature test for auth...
I think there is an error with one of the tests - I think the assertDatabaseCount should be set to
0Would it be better to use
timestamprather thandatetimefor the MySQL column type for thestart_timeandstop_timecolumns? Some tests fail for me due to the expected start_time value including the timezone so it doesn't match up exactly eg..Gavin, in my case i use the format('Y-m-d\TH:i:s.u\Z') method of Carbon to match the start_time. see my comment below.
on your first comment, i agree the code ->assertDatabaseCount('vehicles', 1) should be set to zero.
Gavin, yes, you're probably right, the assertDatabaseCount() is incorrect, will fix in the article (I I'm so glad for text-form courses instead of video...)
As for datetimes, not sure, I haven't encountered this error, maybe you didn't use the
$castsfor those?I added the
$castsbut i'll cross reference my controllers with your completed github repo to double check it :)Last one I promise :)
I didn't seem to get all 3 tests running - they only seemed to all work when I made the following changes to the
ParkingResource.php- can you confirm if I have done something wrong? I basically used a check to see if it was an instance of Carbon and format the date toY-m-d H:i:s.I pushed the repo here if that helps https://github.com/gkimpson/car-parking-api
Weird with those datetimes, as Andy commented elsewhere, and I commented above, I didn't encounter the same errors during my test. Not sure whether it's something about my timezone or something else.
Anyway, your way of solving it is one of the ways, yes.
No worries - I am glad my solution works, you should probably put in a few gotchas like these for every tutorial just to keep me on my toes lol :)
Lol good perspective :)
under testUserCanStopParking(), i have to edit my code from this:
to this:
otherwise, it returns an error because using toDateTimeString() on start_time and stop_time does not return fractional seconds ('.000000Z'). so to fix this, i used format('Y-m-d\TH:i:s.u\Z').
Thanks for being so active in the comments, Andy!
youre welcome Povilas. just trying to help people out whenever i can.
Thanks Andy I like the solution that is brilliant thank you again!
I also did the 'homework' tasks - so anybody that hasn't done this yet and wants to do so without any tips please look away now :)
Povilas - would the tests below appear to look like valid tests for you, if not how would you modify them to make them better?
ProfileTest.php
In the second case, I would probably not do the logout, just trying the request without a user. Also, not sure if
assertGuest()andassertStatus(401)aren't testing the same thing, don't remember what's actually insideassertGuest(), check the docs please :)But overall good job!
Thanks for the feedback slowly getting to understand how testing works so this is great! Really love this text series by the way!!
the correct string in my case (L10) ....
Hello, There is a problem in the
UserCanDeleteTheirVehicletest because thismodelusesSoftDeletes, so the record is not deleted, that way, when you doassertDatabaseCount('vehicles', 0), it displays the error, because the count of the table is 1 and the comparative is 0. In thecontrollerI switched to usingforceDeleteand then the test passed. Did I make a mistake, or did I misunderstand something?You understood it exactly right, now fixed in the lesson. The reason was that SoftDeletes were added to that model later, so there was a mismatch between repo and lesson text. Now should be good.
Good catch!
Thank you!