问题描述
我在 ModelClass 中定义了关系
public function allocations(): MorphMany
{
return $this->morphMany(
Allocation::class,'allocatable','ref_class','ref_id'
)->where('is_active',1); // this line is creating issue
}
测试用例
public function testAllocations(): void
{
// Arrange
$morphMany = $this->partialMock(MorphMany::class);
$model = $this->createPartialMock(
ModelClass::class,['morphMany']
);
// Expectations
$model->expects($this->once())
->method('morphMany')
->with(
Allocation::class,'ref_id'
)
->willReturn($morphMany);
// Action
$result = $model->allocations();
// Assert
$this->assertInstanceOf(MorphMany::class,$result);
}
如果我删除关系中的 where 条件,则测试将正确通过。 我需要关系中的 where 条件。如何在 TestCase 中传递 where 条件?
任何建议都会很棒。谢谢:)
解决方法
你有没有尝试过这样的事情?
$data = $this->morphMany(
Allocation::class,'allocatable','ref_class','ref_id'
);
return $data->where('is_active',1);