Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test if correct layout was used in controller in Rails 3

In Rails 2 I would do

  def assert_layout(layout_name)
    assert_equal layout_name, @response.layout
  end

and:

 assert_layout 'layouts/layout_name'

This doesn't work in rails 3 anymore (undefined method `layout'). How should I change the custom assert_layout method?

like image 225
deb Avatar asked Aug 05 '11 16:08

deb


People also ask

How to test controller in Rails?

The currently accepted way to test rails controllers is by sending http requests to your application and writing assertions about the response. Rails has ActionDispatch::IntegrationTest which provides integration tests for Minitest which is the Ruby standard library testing framework.

How can you tell Rails to render without a layout *?

By default, if you use the :plain option, the text is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the layout: true option and use the . text. erb extension for the layout file.

How to run test cases in Rails?

We can run all of our tests at once by using the bin/rails test command. Or we can run a single test file by passing the bin/rails test command the filename containing the test cases. This will run all test methods from the test case.

What keyword is used in a layout to identify a section where content from the view should be inserted?

Within the context of a layout, <%= yield %> identifies a section where content from the view template should be inserted. The simplest way to use this is to have a single yield, into which the entire contents of the view currently being rendered is inserted.


2 Answers

Use assert_template 'layouts/layout_name'

like image 81
Wizard of Ogz Avatar answered Oct 05 '22 18:10

Wizard of Ogz


Also I've found that if you're doing a test for nil layout you have to do the following...

assert_template :layout => nil
like image 38
Subimage Avatar answered Oct 05 '22 19:10

Subimage