Reputation: 915
I can test my controller to render a certain layout with
expect { get :index }.to render_template(layout: 'my_layout')
But how can I test the controller to render NO layout?
The following first expectation passes, but be careful: the second expectation also passes! (testing the same code)
expect { get :index }.to render_template(layout: false)
expect { get :index }.to render_template(layout: true)
In Nov 2008, @david-chelimsky said:
One way I've handled this successfully is to integrate_views for this one example (in its own group) and specify that html elements from the layout are not present in the form. It's a brittle example, but it's only one.
I dont want to check the rendered view, but I did not find any better solution so far.
Does someone has a good approach?
Upvotes: 13
Views: 5384
Reputation: 11544
I did it using this one liner
expect { get :index }.to render_template(layout: [])
Versions: Rspec = 3.4.0 , Rails ~> 4.2.5
Upvotes: 0
Reputation: 11647
While not a pretty one liner (you can always add a helper method), I've found that you can do this:
get :index
@templates.keys.should include(nil)
I tested this and it only works when I set layout false
. Based on the implementation of assert_template it gathers some information in to instance variables. The relevant ones are @templates
and @layouts
- each is a hash keyed by a string corresponding with how many times it was rendered.
@templates
will contain the template used for your action (e.g "users/show"
) but @layouts
will only list layouts. If no layout was used, it looks like {nil=>1}
. This seems the only thing you can tap in to.
So maybe it might be nice to make a helper method or custom matcher to do this.
Upvotes: 3
Reputation: 1555
In my tests when there is no layout I just check if it is not loading "application" layout
expect { get :index }.to_not render_template(layout: "application")
Upvotes: 4