In many frameworks like Springboot, mocking the database is common practice because it’s an external resource and it can increase latency with testing.
I haven’t seen that so far from Laravel. Is there an offering or documentation around mocking the database calls?
We have tried using some smaller libraries but they are volatile to change and we need an enterprise solution.
2
Answers
Yes, Laravel provides robust capabilities for testing.
If you’re using
PHPUnit
, in phpunit.xmlOr with
RefreshDatabase
, read Quick tip: How to set up in-memory database for Laravel unit testsAs well we have Mocking Facades, Mocking
I would strongly suggest to NOT mock any DB calls, why? Laravel has events, scopes, attributes, etc behind scenes (that you can set), and if you mock one method, maybe a scope is not applied, or anything else, so I always suggest NOT mocking the database.
It is easier to set up a clean test database and migrate it and when all the tests are done, do something with it, than mocking calls and whatever from either
DB
facade or a or more than one class extendingModel
.You can check my profile, I do have some links in there related to other StackOverflow questions about testing, that may help you a lot in general.
Which one is easier for you (I am going to use @Abdulla Nilam‘s example):
Or this more "laravel way" approach:
Of course, these are super simple examples, but 99.9% of the time do not mock database calls at all. You definitely can, but you are very prone to mock something that Laravel processes on the back, as I said before, maybe an event, a scope, an attribute casting, etc.