如何在 Laravel 中模拟 Cache::remember

How to mock Cache::remember in Laravel

在单元测试方法中,我尝试像这样模拟 Cache::remember 响应:

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

但是我得到这个错误:

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

我不明白为什么会出现此错误,而且在 Laravel 文档中也没有找到任何相关信息。说是没有匹配,但是好像是匹配的

如何模拟 Cache::remember 响应?

请参阅有关模拟缓存外观的挑战的问题 -

https://github.com/laravel/framework/issues/10803

也参考

https://github.com/laravel/framework/issues/9729

with 方法的第三个参数替换为 \Closure::class 以匹配任何闭包。

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