如果我有一個Devise模型用戶,其中只有角色爲admin的用戶才能查看某個url,那麼如何編寫RSpec集成測試來檢查該url的狀態是否返回200?如何使用RSpec和Devise/CanCan進行集成測試?
def login(user)
post user_session_path, :email => user.email, :password => 'password'
end
這是僞建議在回答這個問題:Stubbing authentication in request spec,但我不能爲我的生命得到它與色器件的工作。 CanCan在檢查Ability時會收到一個零用戶,這個用戶自然沒有正確的權限。
在集成規範中無法訪問控制器,所以我不能存根current_user,但我想要做這樣的事情。
describe "GET /users" do
it "should be able to get" do
clear_users_and_add_admin #does what it says...
login(admin)
get users_path
response.status.should be(200)
end
end
注意!:自問題提出後,所有這些都發生了變化。要做到這一點目前最好的辦法是在這裏:http://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara
注:這一切都已經改變,因爲有人問。要做到這一點目前最好的辦法是在這裏:http://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara只是重申第一嘆息讀者喜歡我 – 2012-11-25 11:15:20