Ad

How To Mock Cache::remember In Laravel

- 1 answer

Within a unit test method, I tried to mock a Cache::remember response like this:

Cache::shouldReceive('remember')
    ->once()
    ->with('my_key', 120, function() {}) // There are 3 args in remember method
    ->andReturn([]);

But I get this error:

exception 'Mockery\Exception\NoMatchingExpectationException' with message 'No matching handler found for Mockery_0_Illuminate_Cache_CacheManager::remember ("my_key", 120, object(Closure)). Either the method was unexpected or its arguments matched no expected argument list for this method

I don't understand why I got this error and did not found anything in Laravel documentation about this. It says there is no matching, but it seems to match.

How can I mock a Cache::remember response?

Ad

Answer

replace 3rd argument of with method to \Closure::class to matching any closures.

Cache::shouldReceive('remember')
    ->once()
    ->with('my_key', 120, \Closure::class)
    ->andReturn([]);
Ad
source: stackoverflow.com
Ad