Rspec 3.5, Rails 5, Devise 4 - controller is nil in model

205 views
Skip to first unread message

Janusz M

unread,
Jul 19, 2016, 7:43:33 AM7/19/16
to Public Activity
With this in model:

    tracked owner: -> controller, _ { controller.current_user },
               parameters: :tracked_attributes

 
I get:

     NoMethodError:
       undefined method `current_user' for nil:NilClass


Despite I have the user signed_in in specs (I use helpers from Devise ...). It seems like the controller is not being passed to model in tests.

Test (here's a controller spec, but the same happens for view spec):

RSpec.describe SomeController, type: :controller do
  let(:user ...

  context 'user is signed in' do
    before(:each) do
      @request.env["devise.mapping"] = Devise.mappings[:user]
      user = create(:user, :active)
      sign_in user
    end


    describe 'GET #some_action' do
      it 'loads the requested resource ...' do
        get :some_action, params: { id: resource.id }
        expect(assigns(:resource)).to eq(resource)
      end
    end
...


Fails on 'get :some_action'. When I debug it with binding.pry just before that line, I can call controller.current_user and it returns proper user resource.
Reply all
Reply to author
Forward
0 new messages