ruby on rails - Test User Authentication with RSpec -


i work simple rails 4.1.0 app , not use devise. i'm testing application controller:

class applicationcontroller < actioncontroller::base    protected    def current_user     @current_user ||= user.find(session[:user_id]) if session[:user_id]   end    def signed_in?     !!current_user   end   helper_method :current_user, :signed_in?    def current_user=(user)     @current_user = user     session[:user_id] = user.try :id   end    # want test method   def authenticate_user!     render nothing: true, status: :unauthorized unless current_user   end  end 

i have problem authenticate_user! method. full spec see here. method spec:

  describe 'authenticate_user!'     context 'when user logged in'       before { subject.send(:current_user=, create(:user)) }        'do nothing'         expect(subject.send(:authenticate_user!)).to eq nil       end     end      context 'when user not logged in'       controller         before_action :authenticate_user!         def custom           render :text => 'response'         end       end        before         subject.send(:current_user=, nil)         routes.draw { 'custom' => 'anonymous#custom' }         :custom       end       'returns unauthorized status'         expect(response).to be_nil       end     #   before     #     subject.send(:current_user=, nil)     #     subject.send(:authenticate_user!)     #   end     #     #   { render status: :unauthorized }     #   'renders nothing'     #     expect(response).to be_nil     #   end     end    end 

it works when user logged in, when there no 1 tried 2 methods: anonymous controller , tried call method (commented here). tried inherit testapplicationcontroller applicationcontroller , result same anonymous controller. in case error next:

  failure/error: expect(response).to be_nil    expected: nil         got: #<actioncontroller::testresponse:0x007fc03b158580 @mon_owner=nil, @mon_count=0, @mon_mutex=#<mutex:0x007fc03b0e3938>, @stream=#<actiondispatch::response::buffer:0x007fc03b08bc10 @response=#<actioncontroller::testresponse:0x007fc03b158580 ...>, @buf=[" "], @closed=false>, @header={"x-frame-options"=>"sameorigin", "x-xss-protection"=>"1; mode=block", .... 

so returns response despite current_user set nil , there before_action :authenticate_user! in controller. guess ignores it.

in case of

  before     subject.send(:current_user=, nil)     subject.send(:authenticate_user!)   end    { render status: :unauthorized }   'renders nothing'     expect(response).to be_nil   end 

error next:

    failure/error: subject.send(:authenticate_user!)  module::delegationerror:    actioncontroller::rackdelegation#status= delegated @_response.status=, @_response nil: #<applicationcontroller:0x007fe854c592b8 @_action_has_layout=true, @_routes=nil, @_headers={"content-type"=>"text/html"}, @_status=200, @_request=#<actioncontroller::testrequest:0x007fe854c78c80 @env={"rack.version"=>[1, 2], "rack.input"=>#<stringio:0x007fe8544fbf98>, "rack.errors"=>#<stringio:0x007fe8544f0080>, "rack.multithread"=>true, "rack.multiprocess"=>true, "rack.run_once"=>false, "request_method"=>"get", "server_name"=>"example.org", "server_port"=>"80", 

it has no response object set status.

also, in case:

  controller     # before_action :authenticate_user!     def custom       authenticate_user!       render :text => 'response'     end   end 

the error different:

failure/error: render :text => 'response'  abstractcontroller::doublerendererror:    render and/or redirect called multiple times in action. please note may call render or redirect, , @ once per action. note neither redirect nor render terminate execution of action, if want exit action after redirecting, need "redirect_to(...) , return". 

so, render works, 2 times, , throws error.

i found lot of variety similar tests, devise , methods perform.

thanks help.

figured out. point - response should not nil since action answers (renders , not nil - empty string). , render status: :unauthorized wrong too, there no render in spec, it's line controller (i wonder how appears there). right spec method:

context "when user not logged in"   controller(applicationcontroller)     before_action :authenticate_user!     def custom       render text: 'response'     end   end    before     subject.send(:current_user=, nil)     routes.draw { 'custom' => 'anonymous#custom' }     'custom'   end    { should respond_with :unauthorized }   "returns nothing"     expect(response.body).to be_blank   end end 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -