gpt4 book ai didi

php - Mockery 在测试 Laravel 门面时抛出 NoMatchingExpectationException

转载 作者:行者123 更新时间:2023-12-05 06:45:20 24 4
gpt4 key购买 nike

我正在尝试测试我的一个服务类,但无法通过 PHPUnit 测试。

测试失败的相关部分:

File::shouldReceive('put')->with('app/storage/logs/laravel.log', 'New content.')->once()->andReturn(12);

我要测试的代码(简化版):

$date = Carbon::now()->toDayDateTimeString();
$fileContent = sprintf(
"Last maintenance check performed at %s. The old logs have been deleted.\n\n",
$date
);

// $this->fileLocation refers to a string 'app/storage/logs/laravel.log'
return File::put($this->fileLocation, $fileContent);

我遇到的异常:

Time: 1.72 seconds, Memory: 17.50Mb

There was 1 error:

1) Unit\Services\Maintenance\LogCleanerTest::testLogsAreClearedAndUpdated
Mockery\Exception\NoMatchingExpectationException: No matching handler found for Mockery_0_Illuminate_Filesystem_Filesystem::put("app/storage/logs/laravel.log", "Last maintenance check performed at Tue, Jul 15, 2014 12:16 PM. The old logs have been deleted.
"). Either the method was unexpected or its arguments matched no expected argument list for this method


/home/vagrant/Code/MyApp/vendor/mockery/mockery/library/Mockery/ExpectationDirector.php:93
/home/vagrant/Code/MyApp/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:211
/home/vagrant/Code/MyApp/app/Code/Services/Maintenance/LogCleaner.php:19
/home/vagrant/Code/MyApp/app/Code/Services/Maintenance/LogCleaner.php:19
/home/vagrant/Code/MyApp/app/tests/Unit/Services/Maintenance/LogCleanerTest.php:30
/home/vagrant/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:179
/home/vagrant/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:132

有人可以解释我做错了什么吗?任何帮助将不胜感激。

最佳答案

类似于:

File::shouldReceive('put')->with('app/storage/logs/laravel.log', 'Last maintenance check performed at')->once()->andReturn(12);

应该可以。你在 mock 期待“新内容”。作为第二个参数,但您的代码正在发送“上次维护检查...”。我相信如果 mockery 不能匹配确切的字符串,默认情况下会尝试正则表达式匹配。

关于php - Mockery 在测试 Laravel 门面时抛出 NoMatchingExpectationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24754485/

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