JeffreyWay / Laravel-Testing-Decoded

This project is exclusively for reporting typos and errors in the book, "Laravel Testing Decoded."
34 stars 10 forks source link

$this->layout->content breaks tests #82

Open JonoB opened 11 years ago

JonoB commented 11 years ago

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 ?

JeffreyWay commented 11 years ago

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

JonoB commented 11 years ago

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

Here's an old forum post for reference: http://forums.laravel.io/viewtopic.php?id=839