2014-10-03 52 views
2

我試圖編寫一個測試,確保未經身份驗證的用戶無法訪問某個頁面。這裏是我的規格設計和Rspec - 錯誤在控制器測試時測試未經身份驗證的用戶重定向

it "redirects to the sign in page for unauthenticated users" do 
     bob = Fabricate(:user) 
     sign_out bob 
     carolina_skiff = Fabricate(:boat_brand) 
     get :edit, id: carolina_skiff.id 
     expect(response).to redirect_to new_user_session_path 
    end 

當我運行此我得到這個錯誤。

Failure/Error: get :edit, id: carolina_skiff.id 
    ArgumentError: 
     uncaught throw :warden 

看起來好像我無法註銷用戶。我有另一個規範,我正在登錄一個用戶,並且一個正常工作。

it "sets the @boat_brand" do 
     bob = Fabricate(:user) 
     sign_in bob 
     carolina_skiff = Fabricate(:boat_brand) 
     get :edit, id: carolina_skiff.id 
     expect(assigns(:boat_brand)).to eq(carolina_skiff) 
    end 

任何幫助將不勝感激。 。

+0

'sign_out'方法來自哪裏? – Anthony 2014-10-03 14:23:51

+0

這是一個由Devise提供的測試助手方法https://github.com/plataformatec/devise#test-helpers – 2014-10-03 14:25:52

+0

您正在創建一個用戶,然後在簽名之前嘗試簽署它們。或者......也許您正試圖與未經身份驗證的用戶做到這一點,它應該會引發錯誤?或者至少有通知? – 2014-10-03 17:31:24

回答

0

我不知道這是否會幫助,但試圖做色器件重定向的功能測試日誌後,當我遇到類似的問題就來了,這些都是重要的幾點:

  1. 測試準確Devise::SessionsController(如不。UsersController)或如在文檔中描述寫test Devise::SessionsController
  2. 力正確的映射:https://github.com/plataformatec/devise/wiki/How-To:-Test-controllers-with-Rails-3-and-4-(and-RSpec)
  3. 包括設計:: TestHelpers

與重定向抽樣檢測可以是這樣的:

​​

哪裏:user只是定期有效的色器件的用戶,並orders_path在登錄後預計重定向路徑這是不準確的日誌測試,而是從重定向的測試。登錄用戶的登錄頁面(準確地說)。