dsztc99732 2014-07-15 09:25
浏览 152
已采纳

测试Laravel外观时,Mockery抛出NoMatchingExpectationException

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.

",
    $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.

  • 写回答

1条回答 默认 最新

  • dongsetan3216 2014-07-15 09:38
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题