Jer*_*emy 5 php testing mockery
感谢您的时间。我已经从代码/测试中去除了绒毛。
我得到的最远的是 setAttribute 需要两个字符串作为参数,但是我传入了一个 Carbon 对象,作为测试套件的 Mockery 不喜欢它?是这样吗,有没有更好的方法来使用 Mockery/PHPUnit 测试日期?其他测试和代码有效,似乎只有这个测试有问题。
错误
1) Tests\Unit\Services\StageServiceTest::update_stage
Mockery\Exception\NoMatchingExpectationException: No matching handler found for Mockery_13_App_Entities_Subcategory::setAttribute('stage_updated_at', object(Carbon\Carbon)). Either the method was unexpected or its arguments matched no expected argument list for this method
Objects: ( array (
'Carbon\\Carbon' =>
array (
'class' => 'Carbon\\Carbon',
'properties' =>
array (
),
),
))
Run Code Online (Sandbox Code Playgroud)
测试的一点
$subcategory = \Mockery::mock(Subcategory::class);
$stage = \Mockery::mock(Stage::class);
$subcategory
->shouldReceive('setAttribute')
->with('stage_updated_at', Carbon::now())
->once();
$this->service->updateSubcategoryStage(self::SUBCATEGORY_ID, $stageId);
Run Code Online (Sandbox Code Playgroud)
代码的一点
$subcategory->stage_updated_at = Carbon::now();
$subcategory->save();
Run Code Online (Sandbox Code Playgroud)
从您的示例中不清楚是谁在打电话setAttribute。但我猜你可能正在使用魔法设置器。
您的期望失败的原因是因为您正在比较两个不同的对象。来自文档:
Run Code Online (Sandbox Code Playgroud)When matching objects as arguments, Mockery only does the strict === comparison, which means only the same $object will match
您可以使用 hamcrest 匹配器来放松比较equalTo:
$subcategory
->shouldReceive('setAttribute')
->with('stage_updated_at', equalTo(Carbon::now()))
->once();
Run Code Online (Sandbox Code Playgroud)
你仍然会遇到麻烦,因为时间总是稍有偏差。幸运的是,Carbon 提供了一种“立即”修复以进行测试的方法。您只需在测试用例中设置它:
Carbon::setTestNow('2020-01-31 12:13:14');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
204 次 |
| 最近记录: |