r123

Returning paginated results from a mocked repository

Posted 1 week ago by r123

View is failing to render because of what is returned from my mocked repository which makes the test fail. It works fine if I replace mocking with factory that adds entries to database.

In controller I am returning paginated collection.

Controller:

public function index()
{
    $foos = $this->fooRepository->paginate();

    return view('foo.index', compact('foos'));
}

Test:

...
$mock = Mockery::mock(FooRepository::class);
        $mock->shouldReceive('paginate')
            ->once()
            ->andReturn(LengthAwarePaginator::class);

$this->app->instance(FooRepository::class, $mock);

$response = $this->get(route('foo.index'));

$response->assertSuccessful(); // fails response is 500 and response contents has error that $foos is a string and cant call methods on it, etc.
$response->assertViewIs('foo.index');
$response->assertViewHas('foos');
...

This makes view to fail rendering because $foos inside it is a string for some reason.

What do I return from mocked repository to make the test pass?

Please sign in or create an account to participate in this conversation.