Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock Cache::remember in Laravel

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?

like image 229
rap-2-h Avatar asked Mar 14 '23 18:03

rap-2-h


1 Answers

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

Cache::shouldReceive('remember')
    ->once()
    ->with('my_key', 120, \Closure::class)
    ->andReturn([]);
like image 131
hidekuro Avatar answered Mar 21 '23 13:03

hidekuro