First of all, you have to understand why you can't mock the where
method. That's because the class Illuminate\Database\Eloquent\Model
(Eloquent is its alias) doesn't have a where
method explicitly declared. This method is called through the magic methods __callStatic
and __call
and is, in fact, a method of Illuminate\Database\Eloquent\Builder
(sources).
Then, you have several options :
You can accept to call your database in your tests and just define a real context before your assertions. Your tests will be less unitary but we can deal with that in most of the cases.
@michaelcurry solution is a good one. You can build your own abstraction layer with query scopes or other architecture (injecting the query builder by yourself in the model for example) to produce a more testable code.
[never tried] You can mock directly the
DB
facade to completely bypass the database. You will need a good understanding of the Laravel core but it could be a good way to write "pure" unit tests.
Anyway, don't hesitate to dive into the Laravel source code. The code is clear and there are just few classes really important. That's essential to really exploit the power of the framework.