【发布时间】:2023-04-09 00:12:01
【问题描述】:
我正在努力学习如何正确测试,并且在下面的场景中努力理解模拟。我似乎无法模拟一堂课。
主类使用许多组件类来构建特定的活动。我可以自己测试组件并正确模拟它,但是当我尝试在主类中集成测试时,它调用的是真实服务而不是模拟服务。
这是在 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]);
但每次调用真正的服务而不是模拟版本
我正在学习这一点,因此对基本错误表示歉意,但有人可以解释我应该如何正确模拟外部服务
【问题讨论】:
标签: php laravel testing phpunit