2011-01-19 73 views

回答

50

您可以使用anonymous controller來測試你的ApplicationController,作爲RSpec的文檔中描述。還有一個關於testing helpers的部分。

+0

不知何故,我錯過了匿名控制器!謝謝! – Mirko 2011-01-19 19:38:57

+0

正是我需要知道的! – Matthew 2011-11-02 18:13:59

21

您可以在規範中調用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慣用方式。