如何从Rails中的RSpec测试中调用app helper方法?

Hel*_*tos 30 rspec ruby-on-rails

标题是自我解释的.

我尝试的一切都导致了"未定义的方法".

为了澄清,我不是试图测试辅助方法.我试图在集成测试中使用辅助方法.

Chr*_*erg 27

您只需在测试中包含相关的帮助程序模块即可使方法可用:

describe "foo" do
  include ActionView::Helpers

  it "does something with a helper method" do
    # use any helper methods here
Run Code Online (Sandbox Code Playgroud)

它真的很简单.

  • 那是你自己的帮手吗?“SearchHelper”是一个模块吗?如果是这样,只需将 `inlucde ActionView::Helpers` 替换为 `include SearchHelper`,然后您应该就可以使用 `formatResultDate(r.date)` 调用该方法。 (2认同)
  • 在测试邮件时,这对我不起作用.例如`expect(mail.body).to match(I18n.t('some.path',参数:link_to(value,custom_url_helper(value)))`. (2认同)

Mar*_*ine 8

对于迟到这个问题的人,可以在Relish网站上找到答案.

require "spec_helper"

describe "items/search.html.haml" do
  before do
    controller.singleton_class.class_eval do
      protected
      def current_user
        FactoryGirl.build_stubbed(:merchant)
      end
      helper_method :current_user
    end
  end

  it "renders the not found message when @items is empty" do
    render

    expect(
      rendered
    ).to match("Sorry, we can't find any items matching "".")
  end
end
Run Code Online (Sandbox Code Playgroud)