Thursday, 15 September 2011

ruby on rails - Testing an unauthenticated user Rspec/Devise -



ruby on rails - Testing an unauthenticated user Rspec/Devise -

i have had around maybe im not looking in right places. trying find out how test user cannot access page whos controller has

before_filter :authenticate_user!

ideally capture devise message @ same time

"you need sign in or sign before continuing"

i have far

require 'spec_helper' describe campaignscontroller "should not allow user able access without beingness authenticated" :index response.should redirect_to(new_user_session_path) end

end

at nowadays error

failure/error: response.should redirect_to(new_user_session_path) expected response <redirect>, <200>

campaigns controller

class campaignscontroller < applicationcontroller before_filter :authenticate_user! def index @campaigns = campaign.all end end

in spec helper calling 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) @request.env["devise.mapping"] = devise.mappings[:user] user = factorygirl.create(:user) sign_in user end end end

im not calling login_user method @ stage spec_helper phone call this?

how approach correctly

any help appreciated

thanks

at first seems ok. problem caused if run entire suite, , depending on place phone call login_user add together before(:each) all tests.

i understand want minimise typing, prefer tests little more explicit: want see going on in spec.

so how write kind of test:

describe homecontroller include devise::testhelpers context "when not signed in" describe "get 'index'" "redirects sign in" 'index' response.should be_redirect end end describe "get 'about'" "returns http success" 'about' response.should be_redirect end end end context "when signed in" before user = factorygirl.create(:user) sign_in(user) end describe 'get :index' "returns http success" 'index' response.should be_success end end describe "get 'about'" "returns http success" 'about' response.should be_success end end end end

yes, admitted: still fond of old rspec syntax (using should), reads much more natural me.

ruby-on-rails ruby rspec devise

No comments:

Post a Comment