laravel - 无法在 Laravel 中模拟 Cache::put() 外观
问题描述
我试图嘲笑Cache::put()
门面。但这给了我一个错误。我尝试了不同的方法,但无法弄清楚。
public function testGetAllFromDatabase()
{
$industry = new Industry();
Cache::shouldReceive('has')
->once()
->with('industries.all')
->andReturn(false);
Cache::shouldReceive('put')
->with('industries.all', '', 0)
->andReturn(true);
$this->industryMock
->shouldReceive('all')
->once()
->andReturn(array_reverse($this->industries));
$this->app->instance(Industry::class, $this->industryMock);
$industryRepository = new IndustryRepository();
$all = $industryRepository->all();
dd($all);
$this->assertContains( $this->industries[2], $all);
}
但是当我执行它时,会发生以下错误。
$ vendor/bin/phpunit
PHPUnit 7.2.7 by Sebastian Bergmann and contributors.
...E 4 / 4 (100%)
Time: 3.76 seconds, Memory: 12.00MB
There was 1 error:
1) Tests\Unit\RepositoriesTests\IndustryRepositoryTest::testGetAllFromDatabase
Mockery\Exception\NoMatchingExpectationException: No matching handler found for Mockery_1_Illuminate_Cache_CacheManager::put('industries.all', object(Illuminate\Database\Eloquent\Collection), '1440'). Either the method was unexpected or its arguments matched no expected argument list for this method
Objects: ( array (
'Illuminate\\Database\\Eloquent\\Collection' =>
array (
'class' => 'Illuminate\\Database\\Eloquent\\Collection',
'properties' =>
array (
),
),
))
F:\development\consulting.local\src\vendor\mockery\mockery\library\Mockery\ExpectationDirector.php:92
F:\development\consulting.local\src\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php:223
F:\development\consulting.local\src\app\Repositories\IndustryRepository.php:30
F:\development\consulting.local\src\tests\Unit\RepositoriesTests\IndustryRepositoryTest.php:81
我尝试了很多方法,但无法修复它。谢谢你。
解决方案
由于它可以帮助其他人,Laravel 的外观包括帮助函数,允许然后与Mockery
测试替身交换
这意味着当您使用 a 时,shouldReceive
您可以将它与任何Mockery 期望链接起来,例如在这种情况下,如果您不关心可以使用的某些参数:
Cache::shouldReceive('put')
->with('industries.all', \Mockery::any(), \Mockery::any())
->andReturn(true);
推荐阅读
- javascript - 如何在javascript中模拟回车键按钮
- python - 按值提取并使用python将其放在列表的开头
- java - 如何使用 Goertzel 算法检测频率
- java - JavaFx:用加/乘/双向绑定数字
- kubernetes - kubectl 使用 OR 运算符按标签获取资源
- ios - CollectionViewCell 中的 YTSwiftyPlayer
- java - 没有ParameterResolver的JUnit5中BeforeEach,AfterEach的参数化
- android - min-Sdk 除了使用最新 API 的设备外不工作(29)
- sql - SQL:是否有可能过滤两个附加了按时间顺序排列的 CASE 值?
- django - Django 查询集如何通过联合查询集聚合(ArrayAgg)?