Tests using Cache fail with StructuredCache update #627
-
After upgrading laravel-data to version 3.11, some tests that have unrelated FAILED Tests\Unit\Services\Http\RequestTest > ignoring cache BadMethodCallException
Received Mockery_8_Illuminate_Cache_CacheManager::store(), but no expectations were specified
at vendor/spatie/laravel-data/src/Support/Caching/DataStructureCache.php:18
14▕
15▕ public function __construct(
16▕ protected array $cacheConfig,
17▕ ) {
➜ 18▕ $this->store = cache()->store($this->cacheConfig['store'])->getStore();
19▕ $this->prefix = $this->cacheConfig['prefix'] ? "{$this->cacheConfig['prefix']}." : '';
20▕ } The reason I guess is that the Cache is faked and previously laravel-data didn't call the cache.
Here is an example test: public function testProcessCache(): void
{
Cache::expects('get')->once();
// without this it fails
// Cache::expects('store')->andReturnSelf();
// Cache::expects('getStore')->andReturn(new ArrayStore());
$obj = MyDataObject::from(['foo' => 1]);
$this->assertTrue($obj->process());
} Where the I wonder if there's a better way to fix this? Would it be possible for laravel-data to avoid the cache if it's running in the test environment or the cache is faked? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Update: I looked a bit more into this and changed the test code to this: public function testProcessCache(): void
{
// Laravel-data calls Cache::store(), we need to get it before mocking the cache
$store = Cache::store();
Cache::expects('store')->zeroOrMoreTimes()->andReturn($store);
Cache::expects('get')->once();
$obj = MyDataObject::from(['foo' => 1]);
$this->assertTrue($obj->process());
} I'd still like to see if there's a way to disable it during testing, maybe a config value in the |
Beta Was this translation helpful? Give feedback.
-
Hmm good point, I've added an enabled flag in the config you can disable it during testing. Maybe we should never check caches in testing, not sure about that though. |
Beta Was this translation helpful? Give feedback.
Hmm good point, I've added an enabled flag in the config you can disable it during testing. Maybe we should never check caches in testing, not sure about that though.