无法在Laravel中模拟缓存::put()外观

无法在Laravel中模拟缓存::put()外观,laravel,phpunit,laravel-facade,Laravel,Phpunit,Laravel Facade,我试图模拟Cache::put()facade。但这给了我一个错误。我尝试过不同的方法,但没能找到答案 public function testGetAllFromDatabase() { $industry = new Industry(); Cache::shouldReceive('has') ->once() ->with('industries.all') ->andReturn(false);

我试图模拟
Cache::put()
facade。但这给了我一个错误。我尝试过不同的方法,但没能找到答案

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由塞巴斯蒂安·伯格曼和贡献者编写。
…E 4/4(100%)
时间:3.76秒,内存:12.00MB
有1个错误:
1) Tests\Unit\RepositoriesTests\IndustryRepositoryTest::testGetAllFromDatabase
Mockry\Exception\NoMatchingExpectationException:找不到Mockry\u 1\u Illumb\u Cache\u CacheManager::put('industries.all',object(illumb\Database\elounce\Collection),'1440')的匹配处理程序。该方法是意外的,或者其参数与此方法的预期参数列表不匹配
对象:(数组)(
'照亮\\数据库\\雄辩\\收藏'=>
排列(
'class'=>'照亮\\数据库\\雄辩\\收集',
“属性”=>
排列(
),
),
))
F:\development\consulting.local\src\vendor\mockry\mockry\library\mockry\ExpectationDirector.php:92
F:\development\consulting.local\src\vendor\laravel\framework\src\light\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
因为它可以帮助其他人,所以它包含了一些帮助函数,这些函数允许使用双精度测试进行交换

这意味着当您使用
shouldReceive
时,您可以将其与任何参数链接,例如,如果您不关心可以使用的某些参数,则可以使用:

Cache::shouldReceive('put')
    ->with('industries.all', \Mockery::any(), \Mockery::any())
    ->andReturn(true);
因为它可能会帮助其他人,所以包含了允许交换的助手函数,然后使用测试双精度

这意味着当您使用
shouldReceive
时,您可以将其与任何参数链接,例如,如果您不关心可以使用的某些参数,则可以使用:

Cache::shouldReceive('put')
    ->with('industries.all', \Mockery::any(), \Mockery::any())
    ->andReturn(true);

为了帮助他人,最好指出,您可能不想模拟缓存,而是实际使用真正的缓存

这是因为它是一个仅用于测试的缓存:

通过vendor/bin/phpunit运行测试时,Laravel[…]会在测试时自动将会话和缓存配置到阵列驱动程序,这意味着测试时不会保留任何会话或缓存数据。


请注意,与OP的测试不同,您可能需要遵循Laravel关于测试类扩展其
TestCase
的指导来获得行为,例如:

使用PHPUnit\Framework\TestCase;
类ExampleTest扩展了TestCase

如果它对其他人有帮助,最好指出,您可能不想模拟缓存,而是实际使用真正的缓存

这是因为它是一个仅用于测试的缓存:

通过vendor/bin/phpunit运行测试时,Laravel[…]会在测试时自动将会话和缓存配置到阵列驱动程序,这意味着测试时不会保留任何会话或缓存数据。


请注意,与OP的测试不同,您可能需要遵循Laravel关于测试类扩展其
TestCase
的指导来获得行为,例如:

使用PHPUnit\Framework\TestCase;
类ExampleTest扩展了TestCase

put
industries一起调用。所有
以及一个集合和1440作为参数2和3,因此与指定的
''、0
不匹配。如果您不关心参数2和参数3,请使用
$this->anythis()
作为them@apokryfos:你是说Cache::shouldReceive('put')->with('industries.all',$this->anythis(),$this->anythis())->andReturn(true);是的,不过这对我很有用。使用时,您可能需要使用
\mockry::any()
来代替。谢谢,它起了作用。
put
是通过
行业调用的。所有
以及一个集合和1440作为参数2和3,因此与您指定的
'',0
不匹配。如果您不关心参数2和参数3,请使用
$this->anythis()
作为them@apokryfos:你是说Cache::shouldReceive('put')->with('industries.all',$this->anythis(),$this->anythis())->andReturn(true);是的,不过这对我很有用。使用时,您可能需要使用
\mockry::any()
来代替。谢谢,它起作用了。