ruby on rails 4 - Verify user authenticated for actions -


i'm using devise , want check in specs controller actions covered authenticate_user!

i've looked on devise docs, feel there's simple , obvious i'm not getting way devise works.

based on this post built spec check this, doesn't seem working.

when run spec, get:

 failure/error: expect(controller).to receive(:authenticate_user!)     (#<librariescontroller:0x000001035639f8>).authenticate_user!(*(any args))        expected: 1 time arguments        received: 0 times arguments 

libraries_controller_spec.rb

require 'rails_helper'  rspec.describe librariescontroller, type: :controller    let(:valid_attributes) {     skip("add hash of attributes valid model")   }    let(:invalid_attributes) {     skip("add hash of attributes invalid model")   }    let(:valid_session) { {} }    describe "get #index"     "should authenticated"       :index, {}       expect(controller).to receive(:authenticate_user!)     end   end end 

and controller itself:

libraries_controller.rb

class librariescontroller < applicationcontroller   before_action :set_library, only: [:show, :edit, :update, :destroy]   before_action :authenticate_user!    # /libraries   # /libraries.json   def index     @libraries = library.all   end    private     # use callbacks share common setup or constraints between actions.     def set_library       @library = library.find(params[:id])     end end 

well embarassing. turns out had expectation , method call in wrong order in spec. should've been:

describe "get #index"   "should authenticated"     expect(controller).to receive(:authenticate_user!)     :index, {}   end end 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -