我曾經環顧四周,但也許我沒有在正確的位置尋找。我試圖找出如何測試一個用戶不能訪問一個頁面衛生組織控制器具有測試未經身份驗證的用戶Rspec/Devise
before_filter :authenticate_user!
理想我想在同一時間捕捉色器件消息
"you need to sign in or sign up before continuing"
我有這如此遠
require 'spec_helper'
describe CampaignsController do
it "should not allow a user be able to access without being authenticated" do
get :index
response.should redirect_to(new_user_session_path)
end
末
目前我得到的錯誤
Failure/Error: response.should redirect_to(new_user_session_path)
Expected response to be a <redirect>, but was <200>
廣告活動控制器
class CampaignsController < ApplicationController
before_filter :authenticate_user!
def index
@campaigns = Campaign.all
end
end
在我的天賦幫助我打電話以下
# Include Devise Test Helpers
config.include Devise::TestHelpers, :type => :controller
config.extend ControllerMacros, :type => :controller
controller_macros.rb
module ControllerMacros
def login_user
before(:each) do
@request.env["devise.mapping"] = Devise.mappings[:user]
user = FactoryGirl.create(:user)
sign_in user
end
end
end
我不是在調用這個方法login_user階段,所以將spec_hel每通電話這個?
怎樣運用這個正確
任何幫助表示讚賞
感謝
請發表'CampaignsController'。 –
似乎對我來說,你能確定你沒有在某處(你在spec_helper中)調用'sign_in'方法嗎?我們是否也可以擁有ApplicationController的代碼? – ccyrille
更新,現在思考你的意見後,你可以在 – Richlewis