Rails 2.3.x - 如何在功能测试(无 RSpec)中存根辅助方法(从视图调用)?

发布于 2024-11-08 12:43:01 字数 1456 浏览 0 评论 0原文

请不要告诉我“搜索更多”或其他内容,导致类似问题的所有解决方案都失败。

简单的: 我有一个功能测试。我想做一个简单的获取,看看是否渲染了正确的内容

  test "displays headline if user should see it" do
    get :index  
    assert_match /headline/, response.body
  end

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

以及一个简单的视图

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

和一个助手:

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

所以我需要做的是在测试中做类似的事情:

  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

问题是什么是什么?我想存根它,因为我在单元/助手中测试了助手。

在 get 帮助器模块重新混合到控制器类中之后。请不要给我其他答案的链接,我读过它们(但当然我可能读错了)并且它们对我不起作用。我使用 Rails 2.3.10 和 mocha 0.9.8。

不起作用的事情:

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

更新:

唯一有效的模拟是:

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

但我当然不会使用它......也许这是一个线索

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
  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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文