[英]How to test ApplicationController method defined also as a helper method?
在我的ApplicationController中,我有一個定義為輔助方法的方法:
helper_method :some_method_here
我正在使用Rails3和RSpec2
您可以在規范中的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
雖然@controller
和subject
可以在這里互換使用,但我現在會將subject
作為RSpec慣用的方式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.