簡體   English   中英

Mockery在測試Laravel Facade時拋出NoMatchingExpectationException

[英]Mockery throws NoMatchingExpectationException when testing Laravel facade

我正在嘗試測試我的服務類之一,但無法通過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);

應該管用。 您是在嘲笑期待“新內容”。 作為第二個參數,但是您的代碼正在發送“上次維護檢查...”。 我相信嘲諷會在默認情況下嘗試匹配正則表達式,如果它不能匹配確切的字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM