2011-02-27 116 views
4

我已經開始爲我的控制器編寫測試。對於其中一次,我需要登錄,以檢查頁面是否呈現等。Rspec Rails - 模擬成爲current_user來測試控制器

因爲我使用的Devise在用戶模型上有一個current_user helper,我該如何形成我的:

describe "when it GETS 'town'" do 
    it "should render the template and be successful" do 
     get :index 
     response.should render_template(:index) 
     response.should be_success 
    end 
end 

這樣我就不會得到未定義的「驗證!」方法(設計特定)錯誤?

回答

14

Devise wiki上有關於此主題的很好的寫法。基本上你需要添加設計測試助手,然後調用sign_in來獲得一個有效的當前用戶,當你的規範期望一個。