gpt4 book ai didi

Laravel 功能测试。缓存模拟不起作用! CacheManager::driver(),但未指定预期

转载 作者:行者123 更新时间:2023-11-28 21:17:04 27 4
gpt4 key购买 nike

当我使用单元测试缓存移动来测试我的应用程序时,效果很好。但是当我尝试通过功能测试来测试我的 api 时,我遇到了一个异常。

public function testGet()
{
Cache::shouldReceive('rememberForever')->times(5)->andReturn([]);

Cache::shouldReceive('has')
->once()
->andReturn(false);

Cache::shouldReceive('forever')
->once()
->andReturn([]);

$response = $this->getJson('/api/table/get');
$response->assertOk();

$responseArr = $response->getOriginalContent();
$this->assertEmpty($responseArr['table']);
}

Tests\Feature\Controller\API\Site\TableApiControllerTest::testGet Mockery\Exception\BadMethodCallException: Received Mockery_2_Illuminate_Cache_CacheManager::driver(), but no expectations were specified

在其他单元测试中完全相同的模拟工作没有错误。

最佳答案

我在这里找到了答案 - https://github.com/laravel/framework/issues/10803#issuecomment-401611084%23issuecomment-401611084

    $cacheDriver = app('cache')->driver();
Cache::shouldReceive('driver')->andReturn($cacheDriver);

关于Laravel 功能测试。缓存模拟不起作用! CacheManager::driver(),但未指定预期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56234834/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com