开发者

Rails 2.3.x - How to stub a helper method (that gets called from a view) in a functional test (no RSpec)?

开发者 https://www.devze.com 2023-03-06 23:58 出处:网络
Please don\'t tell me \"search more\" or other stuff cause all solutions for similar question fail. Simple:

Please don't tell me "search more" or other stuff cause all solutions for similar question fail.

Simple: I have a functional tests. I want to make a simple get and see if proper content gets rendered

  test "displays headline if user should see it" do
    get :index  
    assert_match /headline/, response.body
  开发者_JAVA技巧end

  test "doesn't display headline if user shouldn't see it" do
    get :index
    assert_no_match /headline/, response.body
  end

and a simple view

  <% if show_headline?(arg) %>
    headline
  <% end %>

and a helper:

  module TheHelper
    def show_headline?(arg)
      arg ? hard_code_logic : even_harder_logic
    end
  end

so what I need is to do in test something like:

  test "displays headline if user should see it" do
    Something.stubs(:show_headline?).returns(true)

    get :index  
    assert_match /headline/, response.body
  end

  test "doesn't display headline if user shouldn't see it" do
    Something.stubs(:show_headline?).returns(false)

    get :index
    assert_no_match /headline/, response.body
  end

The question is what is Something? I want to stub it cause I have helpers tested in unit/helpers.

After the get helper module gets remixed into the controller class. Please don't give me links to other answers, I read them (but of course I could have read the wrong ones) and they don't work for me. I use Rails 2.3.10 with mocha 0.9.8.

Things that don't work:

TheController.any_instance.stubs(:show_headline?)
ActionView::Base.any_instance...
@controller.stubs...

UPDATE:

the only mock that worked was:

  <% self.stubs(:show_headline?).returns(true) >%
  <% if show_headline?(arg) %>
    headline
  <% end %>

but of course I will not use that... maybe it is a clue

0

精彩评论

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

关注公众号