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?
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.
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.
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.
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.
Use assert_template 'layouts/layout_name'
Also I've found that if you're doing a test for nil layout you have to do the following...
assert_template :layout => nil
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With