1

I have defined relationship in the ModelClass

public function allocations(): MorphMany
{        
    return $this->morphMany(
        Allocation::class,
        'allocatable',
        'ref_class',
        'ref_id'
    )->where('is_active', 1); // this line is creating issue
}

Test Case

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,
            'allocatable',
            'ref_class',
            'ref_id'
        )
        ->willReturn($morphMany);
    // Action
    $result = $model->allocations();
    // Assert
    $this->assertInstanceOf(MorphMany::class, $result);
}

Error: Call to a member function where() on null

If I remove where condition in the relationship, the test is passing correctly. I need that where condition in the relationship. How can I pass where condition in TestCase?

Any suggestion will be great. Thanks :)

Ravi Hirani
  • 6,511
  • 1
  • 27
  • 42

1 Answers1

0

Have you tried something like this?

$data = $this->morphMany(
        Allocation::class,
        'allocatable',
        'ref_class',
        'ref_id'
    );

return $data->where('is_active', 1);