Dwight Watson's blog

Assert view template used in Laravel 4

This blog post was originally published a little while ago. Please consider that it may no longer be relevant or even accurate.

Some of the controllers in a project I work on return different view templates depending on certain requirements. For example, the search() method returns View::make('posts.search') if no input is provided, but View::make('posts.results') if a query string parameter is passed for the purpose of testing. However, there is no built-in function for asserting which view template is rendered when writing your functional tests. Here is a handy function you can pop into your TestCase class to allow you to ensure the correct view template is being rendered when you need.

/**
* Tests to see whether the view template provided is the one
* used in rendering the view.
*
* @param string
* @return void
*/
protected function assertViewIs($templateName)
{
return $this->assertEquals($templateName, $this->client->getResponse()->original->getName());
}

A blog about Laravel & Rails by Dwight Watson;

Picture of Dwight Watson

Follow me on Twitter, or GitHub.