$this->layout->content breaks tests
All of my controller methods set up the view using the following kind of syntax:
$this->layout->content = View::make('posts.index', $posts)
I am unit testing this with the following code:
$response = $this->call('GET', 'posts');
$this->assertTrue($this->client->getResponse()->isOk());
$this->assertViewHas('posts');
And this fails with the following error:
Failed asserting that an array has the key 'posts'
If I change my controller method as follows, then the test passes correctly:
return View::make('posts.index', $posts);
Am I missing something obvious here or is it not possible to unit test methods that generate a view with $this->layout ?
I'll have to look into that. That said, the recommended way is to use blade templates, rather than setting $this->layout->content.
Actually, there are two pretty big benefits to using $this->layout->content
- No need to use
@extendsin every view - if you want to change your template, you only need change it in one place, instead of search and replace through many files.
Here's an old forum post for reference: http://forums.laravel.io/viewtopic.php?id=839