Laravel-Testing-Decoded
Laravel-Testing-Decoded copied to clipboard
How to test eager loading?
I've organized my models into repositories, just like you describe in the book, but there's one thing I still don't know how to mock and test; eager loading.
In my controller I have:
public function index()
{
// $this->photoalbum is a repo injected into __construct
return $this->photoalbum->with('sections', 'photos')->get();
}
In my test:
public function testCanShowAllPhotoalbums()
{
$photoalbum = m::mock('Rambonnet\Storage\Photoalbum\PhotoalbumRepositoryInterface');
$photoalbum->shouldReceive('with')
->with('sections', 'photos')
->once()->andReturn($photoalbum);
// what should it return? I'm not sure I'm even interested...
$photoalbum->shouldReceive('get')->once()->andReturn('I have no idea what to return');
$this->app->instance('Rambonnet\Storage\Photoalbum\PhotoalbumRepositoryInterface', $photoalbum);
$this->get('admin/photoalbums');
$this->assertResponseOk();
}
Returning $photoalbum from shouldReceive('with') makes the test pass. But somehow I feel I'm doing this the wrong way, a way that'll bite me soon.
Any thoughts on how I should proceed with these kinds of tests?