13

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?

3 Answers 3

4

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")
2
  • 3
    If it loaded another template this would pass and be a false positive.
    – MrDanA
    May 16, 2013 at 17:37
  • Like I said, it is just to check if it is not loading "application". If there is an answer that solve this in a correct way I will also adopt it :) May 16, 2013 at 19:08
3

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.

5
  • Thanks for that, I went with the line @layouts.keys.should == [nil] for my code (for now). Maybe will circle back for a custom matcher once this problem comes up again :) May 27, 2013 at 0:57
  • Glad you got something to work. Remember to mark this as the answer so future visitors know it worked!
    – MrDanA
    May 27, 2013 at 3:04
  • Alas, not my question, otherwise I would :) May 27, 2013 at 5:13
  • Oh sorry I didn't even notice ha.
    – MrDanA
    May 27, 2013 at 13:01
  • Sorry for coming back so late to read the answers. I will try that and will mark it then. Thanks Robin
    – Robin
    May 28, 2013 at 9:32
0

I did it using this one liner

expect { get :index }.to render_template(layout: [])

Versions: Rspec = 3.4.0 , Rails ~> 4.2.5

1
  • This actually does not work (or creates false-positives might be more accurate). If you define your layout at the controller level - i.e. layout 'whatever' outside of any action method - and then only use render layout: false on any actions where you don't want a layout to be used (such as if you're using a modal), then this test will pass for all of your actions.
    – jeffdill2
    Mar 21, 2016 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.