无法在 Laravel 中模拟 Cache::put() 外观
Unable to mock Cache::put() facade in Laravel
我正在尝试模拟 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's facade includes helper functions that allow swapping then with a Mockery
测试双人
这意味着当您使用 shouldReceive
时,您可以将它与任何 Mockery expectation 链接起来,例如在这种情况下,如果您不关心某些参数,您可以使用:
Cache::shouldReceive('put')
->with('industries.all', \Mockery::any(), \Mockery::any())
->andReturn(true);
如果对其他人有帮助,最好指出 您可能 不想 模拟 Cache,而是实际使用真正的缓存。
那是因为它是一个仅供测试的缓存:
When running tests via vendor/bin/phpunit, Laravel [....] automatically configures the session and cache to the array driver while testing, meaning no session or cache data will be persisted while testing.
https://laravel.com/docs/8.x/testing#environment
请注意,与 OP 的测试不同,您可能需要遵循 Laravel 关于您的测试的指导 class 扩展他们的 TestCase
以获得行为,例如
use PHPUnit\Framework\TestCase;
class ExampleTest extends TestCase
我正在尝试模拟 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's facade includes helper functions that allow swapping then with a Mockery
测试双人
这意味着当您使用 shouldReceive
时,您可以将它与任何 Mockery expectation 链接起来,例如在这种情况下,如果您不关心某些参数,您可以使用:
Cache::shouldReceive('put')
->with('industries.all', \Mockery::any(), \Mockery::any())
->andReturn(true);
如果对其他人有帮助,最好指出 您可能 不想 模拟 Cache,而是实际使用真正的缓存。
那是因为它是一个仅供测试的缓存:
When running tests via vendor/bin/phpunit, Laravel [....] automatically configures the session and cache to the array driver while testing, meaning no session or cache data will be persisted while testing.
https://laravel.com/docs/8.x/testing#environment
请注意,与 OP 的测试不同,您可能需要遵循 Laravel 关于您的测试的指导 class 扩展他们的 TestCase
以获得行为,例如
use PHPUnit\Framework\TestCase;
class ExampleTest extends TestCase