我正在尝试学习如何正确测试,并且正在努力了解下面场景中的模拟。我似乎无法 mock 一个类。
主类使用多个组件类来构建特定的事件。我可以自己测试组件并正确地模拟它,但是当我尝试在主类中集成测试时,它会调用真实服务而不是模拟服务。
这是在 Laravel 5.5 应用程序中。
我有一个基类:
class booking {
private $calEventCreator
public function __construct(CalenderEventCreator $calEventCreator) {
$this->calEventCreator = $calEventCreator;
}
}
然后由另一个类扩展:
class EventType extends booking {
//do stuff
}
CalenderEventCreator 依赖于我想模拟的外部服务。
class CalendarEventCreator {
public function __construct(ExternalService $externalService) {
$this->externalService = $externalService;
}
}
在我的测试中,我尝试执行以下操作:
public function test_complete_golf_booking_is_created_no_ticket()
{
$this->booking = \App::make(\App\Booking\EventType::class);
$calendarMock = \Mockery::mock(ExternalService::class);
$calendarMock->shouldReceive([
'create' => 'return value 1',
])->once();
$this->booking->handle($this->attributes, 'booking');
但是在尝试执行测试时很明显 ExyernalService
没有使用模拟对象。
我尝试重新安排代码如下:
$calendarMock = \Mockery::mock(Event::class);
$calendarMock->shouldReceive([
'create' => 'return value 1',
])->once();
$this->booking = \App::make(\App\Booking\EventType::class);
$this->booking->handle($this->attributes, 'booking');
}
并尝试过:
$this->booking = \App::make(\App\Booking\EventType::class, ['eventService'=>$calendarMock]);
但每次调用的都是真实服务而不是模拟版本
我正在学习这个,所以对基本错误表示歉意,但有人可以解释我应该如何正确地模拟外部服务
最佳答案
也许有更好的方法来实现这一点,但我正在使用以下方法:
$calendarMock = \Mockery::mock(ExternalService::class);
$calendarMock->shouldReceive([
'create' => 'return value 1',
])->once();
$this->booking = new \App\Booking\EventType($calendarMock);
$this->booking->handle($this->attributes, 'booking');
我没有使用服务容器来解析类,而是直接调用构造函数传递模拟服务。
更新
寻找其他方法来做到这一点,我找到了 this answer .使用该解决方案,您的代码将如下所示:
$calendarMock = \Mockery::mock(ExternalService::class);
$calendarMock->shouldReceive([
'create' => 'return value 1',
])->once();
$this->app->instance(ExternalService::class, $mock);
$this->booking = \App::make(\App\Booking\EventType::class);
$this->booking->handle($this->attributes, 'booking');
关于php - 如何在 php 测试中使用模拟对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49480999/