开发者

testing a that Rails controller chooses a layout without rendering the layout

开发者 https://www.devze.com 2023-03-31 21:13 出处:网络
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

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 开发者_如何学Goa 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)


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
0

精彩评论

暂无评论...
验证码 换一张
取 消