1

i'd like to stub out render and i'd like to test that a certain layout is chosen

bonus catch: in the test env, that layout file will not exist

is there a clever way to detect if the controller has selected a layout without rendering, and without triggering an ActionView::MissingTemplate error?

(this is on a Rails 2.3 app, but feel free to chat rails 3)

whatbird
  • 1,552
  • 1
  • 14
  • 25

1 Answers1

1

The easiest way to do this is put your layout selection logic in a helper and test the helper directly. No need to stub anything out or fake the rendering.

class MyController < ActionController::Base
  layout :choose_layout

private
  def choose_layout
    if some_thing?
      'this_layout'
    else
      'other_layout'
    end
  end
end

class MyControllerTest < ActionController::TestCase
  test "choose_layout" do
    @controller.stubs(:some_thing? => true)
    assert_equal 'other_layout', @controller.send(:choose_layout)
  end
end
Winfield
  • 18,985
  • 3
  • 52
  • 65
  • 1
    thanks. i have an aversion to testing a private method like that, but it seems like that's what i've got to do in this case. – whatbird Aug 31 '11 at 18:10
  • 1
    If calling the private method bothers you, you could encapsulate the functionality in a helper class and test that as a unit test. Isolate the logic into a single responsibility piece of code and test that, that's the meta-answer. – Winfield Aug 31 '11 at 18:14