简体   繁体   中英

Mockery throws NoMatchingExpectationException when testing Laravel facade

I'm trying to test one of my service classes but can't get a PHPUnit test to pass.

The relevant part of the test that is failing:

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

The code I'm trying to test (simplified version):

$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);

The exception that I'm getting:

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

Can someone explain what am I doing wrong? Any help will be appreciated.

Something like:

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

Should work. You're telling mockery to expect 'New Content.' as the second parameter, but your code is sending 'Last maintenance check...'. I believe mockery will try a regex match by default if it can't match the exact string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM