Laravel-Testing-Decoded icon indicating copy to clipboard operation
Laravel-Testing-Decoded copied to clipboard

$this->layout->content breaks tests

Open JonoB opened this issue 12 years ago • 2 comments

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 ?

JonoB avatar Aug 03 '13 17:08 JonoB

I'll have to look into that. That said, the recommended way is to use blade templates, rather than setting $this->layout->content.

JeffreyWay avatar Aug 05 '13 18:08 JeffreyWay

Actually, there are two pretty big benefits to using $this->layout->content

  • No need to use @extends in 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

JonoB avatar Aug 06 '13 05:08 JonoB