我試圖爲使用Devise進行身份驗證的Rails應用程序生成一個簡單的宏。基本上我想確保當用戶訪問需要認證的頁面時,他們被重定向到登錄頁面。所以像這樣:爲控制器完善一個RSpec宏
it_requires_authentication_for :index, :new, :create, :update
這裏的預期結果應該是顯而易見的。然而,我的問題是,我想不出來映射每個動作到其相應的HTTP方法的最佳途徑(:獲取,張貼等)
我開始了這一點:
def it_should_require_authentication_for(*actions)
actions.each do |action|
it "should require authentication for #{action}" do
get action.to_sym
response.should redirect_to(new_user_session_path)
end
end
end
哪一個當然只能得到。有人能告訴我如何爲所有行爲提供這個宏嗎?我假設我需要以某種方式測試該動作是否適合某種特定方法,但我只是不確定。
任何幫助,非常感謝。