4
我在鐵路應用程序中使用Devise。用戶和管理員登出設計並通過RSPEC進行測試
我有兩種模式:用戶和管理員。
目前,我可以,如果我訪問的用戶登錄,並在管理標誌登錄既是用戶和管理員。
我想迫使一個的退出,如果在其他標誌。什麼是最好的方法來做到這一點?
此外,如果有人有一個很好的方法來測試rspec /水豚,請分享!
感謝
我在鐵路應用程序中使用Devise。用戶和管理員登出設計並通過RSPEC進行測試
我有兩種模式:用戶和管理員。
目前,我可以,如果我訪問的用戶登錄,並在管理標誌登錄既是用戶和管理員。
我想迫使一個的退出,如果在其他標誌。什麼是最好的方法來做到這一點?
此外,如果有人有一個很好的方法來測試rspec /水豚,請分享!
感謝
我會複製在設計控制器和濾波器之前增加一個像這樣:
class Users::SessionsController < Devise::SessionsController
before_filter :logout_admin, :only => "create"
def create
super
end
private
def logout_admin
# change :admin to :user in the admin's session controller
# the sign_out is a devise controller helper that forces sign out
sign_out :admin
end
end
然後進行測試,你可以在功能(控制器)訪問當前用戶的規格致電主題.current_user或subject.current_admin,去這裏:https://github.com/plataformatec/devise,以確保包括測試助手然後再嘗試這樣的事:
require 'spec_helper'
describe Users::SessionsController do
login_admin
describe "POST create" do
it "should logout admin" do
post :create, {:user => {:email => "[email protected]", :password => "secret"} }
subject.current_user.should_not be_nil
subject.current_admin.should be_nil
end
end
end
我認爲這會工作。你如何在水豚中測試這樣的東西?有沒有特定的幫手? – jignesh 2012-02-29 08:36:10
啊,很好的問題,據我所知你不能從請求規範訪問current_user(這可能不是最好的做法),是否有一部分你的應用程序的視圖是不同的,當用戶登錄vs管理員,還是某種形式的flash [:notice]來幫助區分? – 2012-02-29 08:40:20
所以對於上面的示例代碼,我無法做到subject.current_user然後,正確?我正在考慮使用flash [:notice]通知用戶/管理員他們已經註銷,並且新用戶/管理員已經登錄。我可以在capybara中捕獲該用戶/管理員。 – jignesh 2012-02-29 08:47:32