I have had a look around but maybe I'm not looking in the right places. I am trying to find out how to test that a user cannot access a page whose controller has:
before_filter :authenticate_user!
Ideally I would like to capture the devise message at the same time:
"you need to sign in or sign up before continuing"
I have this so far
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
end
At present I get the error
Failure/Error: response.should redirect_to(new_user_session_path)
Expected response to be a <redirect>, but was <200>
Campaigns Controller
class CampaignsController < ApplicationController
before_filter :authenticate_user!
def index
@campaigns = Campaign.all
end
end
In my spec helper I am calling the following
# 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
I'm not calling the login_user method at this stage, so would the spec_helper call this?
How do I approach this correctly?
CampaignsController
. – Зелёныйsign_in
method somewhere (in you spec_helper?). Could we also have the code of the ApplicationController ? – ccyrille