簡體   English   中英

如何測試ApplicationController方法也定義為輔助方法?

[英]How to test ApplicationController method defined also as a helper method?

在我的ApplicationController中,我有一個定義為輔助方法的方法:

helper_method :some_method_here

  • 我如何在RSpec中測試ApplicationController?
  • 在測試我的視圖/幫助程序時,如何包含/調用此幫助程序方法?

我正在使用Rails3和RSpec2

您可以使用匿名控制器來測試ApplicationController,如RSpec文檔中所述。 還有一個關於測試助手的部分。

您可以在規范中的subject@controller上調用輔助方法。

我一直在尋找這個問題的解決方案,匿名控制器不是我想要的。 假設你有一個生活在app/controllers/application_controller.rb有一個簡單的方法,它沒有綁定到REST路徑:

class ApplicationController < ActionController:Base

  def your_helper_method
    return 'a_helpful_string'
  end

end

然后,您可以在spec/controllers/application_controller_spec.rb編寫測試,如下所示:

require 'spec_helper'

describe ApplicationController do

  describe "#your_helper_method" do
    it "returns a helpful string" do
      expect(subject.your_helper_method).to eq("a_helpful_string")
    end
  end

end

雖然@controllersubject可以在這里互換使用,但我現在會將subject作為RSpec慣用的方式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM