你好,我试图让我的第一次模拟或存根测试与Prophecy一起工作。我从未使用过模拟和存根或Mockery。我已经完成了相当数量的单元测试,其中依赖关系并没有真正发挥作用等。
如果我可以让这个测试工作,我认为它会帮助我进一步测试我的代码。任何帮助将不胜感激!
首先是错误......
Method `Double\App\Services\Maintenance\Flags\MaintenanceFlagsProvider\P1::findMostRecentByLabel()` is not defined.
/Library/WebServer/App/tests/unit/MaintenanceStatusTests.php:21
以下是测试类的相关部分......
use App\Services\Maintenance\Logs\MaintenanceLogProvider;
use App\Services\Maintenance\Flags\MaintenanceFlagsProvider;
use App\Services\Maintenance\NextDueSchedules\NextDueScheduleProvider;
use App\Services\Components\Requirements\Status\RequirementStatusProvider;
use App\Services\Components\Requirements\Properties\ComponentRequirementPropertiesProvider;
class MaintenanceStatusTests extends TestCase
{
/** @test */
public function is_initial_returns_true()
{
$status = $this->buildStatus();
$status->maintenance_flags_provider->shouldReceive('findMostRecentByLabel')->andReturn(new MaintenanceFlag(['value' => 1]));
$this->assertTrue($status->isInitial());
}
private function buildStatus()
{
$maintenance_flags_provider = $this->prophesize(MaintenanceFlagsProvider::class);
$maintenance_log_provider = $this->prophesize(MaintenanceLogProvider::class);
$next_due_schedule_provider = $this->prophesize(NextDueScheduleProvider::class);
$component_requirement_properties_provider = $this->prophesize(ComponentRequirementPropertiesProvider::class);
return new RequirementStatusProvider($maintenance_flags_provider->reveal(), $maintenance_log_provider->reveal(), $next_due_schedule_provider->reveal(), $component_requirement_properties_provider->reveal());
}
}
以下是我尝试测试......
的课程/方法的相关部分namespace App\Services\Components\Requirements\Status;
use Carbon, StdClass;
use App\Services\Maintenance\Logs\MaintenanceLogProvider;
use App\Services\Maintenance\Flags\MaintenanceFlagsProvider;
use App\Services\Maintenance\NextDueSchedules\NextDueScheduleProvider;
use App\Services\Components\Requirements\Properties\ComponentRequirementPropertiesProvider;
class RequirementStatusProvider
{
public $maintenance_flags_provider;
public $maintenance_log_provider;
public $next_due_schedule_provider;
public $component_requirement_properties_provider;
public $data;
/**
* @param MaintenanceFlagsProvider $maintenance_flags_provider
* @param MaintenanceLogProvider $maintenance_log_provider
* @param NextDueScheduleProvider $next_due_schedule_provider
* @param ComponentRequirementPropertiesProvider $component_requirement_properties_provider
*/
public function __construct(MaintenanceFlagsProvider $maintenance_flags_provider, MaintenanceLogProvider $maintenance_log_provider, NextDueScheduleProvider $next_due_schedule_provider, ComponentRequirementPropertiesProvider $component_requirement_properties_provider)
{
$this->data = new StdClass();
$this->maintenance_flags_provider = $maintenance_flags_provider;
$this->maintenance_log_provider = $maintenance_log_provider;
$this->next_due_schedule_provider = $next_due_schedule_provider;
$this->component_requirement_properties_provider = $component_requirement_properties_provider;
}
// THIS IS WHAT I'M TRYING TO TEST ....
public function isInitial()
{
$flag = $this->maintenance_flags_provider->findMostRecentByLabel('initial', $this->component->id, $this->requirement->id, $this->data->datetime);
return (($flag && 1 == $flag->getAttribute('value')) || ! $this->data->pcw_logs[0]->event_at) ? 1 : 0;
}
}
我还以下列方式修改了测试...
/** @test */
public function is_initial_returns_true()
{
$maintenance_log_provider = $this->prophesize(MaintenanceLogProvider::class);
$maintenance_flags_provider = $this->prophesize(MaintenanceFlagsProvider::class);
$next_due_schedule_provider = $this->prophesize(NextDueScheduleProvider::class);
$component_requirement_properties_provider = $this->prophesize(ComponentRequirementPropertiesProvider::class);
$maintenance_flags_provider->findMostRecentByLabel('initial', 1, 1, Carbon::now())->willReturn(new MaintenanceFlag(['value' => 1]));
$status = new RequirementStatusProvider($maintenance_flags_provider->reveal(), $maintenance_log_provider->reveal(), $next_due_schedule_provider->reveal(), $component_requirement_properties_provider->reveal());
$this->assertTrue($status->isInitial());
}
即使我省略了 - > willReturn部分,我仍然会得到完全相同的错误。
我不希望isInitial调用实际的MaintenanceFlagsProvider类,我希望测试伪造该调用但是将$ flag变量设置为我想要的任何测试(如果这是有意义的话)。
答案 0 :(得分:0)
部分问题在于我的提供程序将未定义的方法调用转发到相应的存储库类。通过向提供者类
添加缺少方法的@method签名,我能够使Prophecy感到高兴/**
* @method findMostRecentByLabel(array $args = [])
*/
class MaintenanceFlagsProvider
{
...
}
工作测试......
/** @test */
public function is_initial_returns_true()
{
$date = Carbon::now();
$maintenance_log_provider = $this->prophesize(MaintenanceLogProvider::class);
$maintenance_flags_provider = $this->prophesize(MaintenanceFlagsProvider::class);
$next_due_schedule_provider = $this->prophesize(NextDueScheduleProvider::class);
$component_requirement_properties_provider = $this->prophesize(ComponentRequirementPropertiesProvider::class);
$maintenance_flags_provider->findMostRecentByLabel('initial', 1, 1, $date)->shouldBeCalled()->willReturn(new MaintenanceFlag(['value' => 1]));
$status = new RequirementStatusProvider($maintenance_flags_provider->reveal(), $maintenance_log_provider->reveal(), $next_due_schedule_provider->reveal(), $component_requirement_properties_provider->reveal());
$status->component = (new Component())->forceFill(['id' => 1]);
$status->requirement = (new MaintenanceRequirement())->forceFill(['id' => 1]);
$status->data->datetime = $date;
$this->assertTrue($status->isInitial());
}