0
votes

I am trying to spec the following.

I need to return all entities that are linked to the logged in user. Subsequently I need to create the user before the fact and then ensure that the specific user is logged in. I am struggling to achieve this with controller macros. My specs are failing as follows

1) Yougov::Surveys::ProfilesController GET :index returns all profiles linked to the loged in user with the same country and client as the linked survey Failure/Error: sign_in user

 RuntimeError:
   Could not find a valid mapping for nil
 # /Users/donovan.thomson/.rvm/gems/ruby-2.2.2@insight-app/gems/devise-2.2.8/lib/devise/mapping.rb:42:in `find_scope!'
 # /Users/donovan.thomson/.rvm/gems/ruby-2.2.2@insight-app/gems/devise-2.2.8/lib/devise/test_helpers.rb:46:in `sign_in'
 # ./spec/support/controller_macros.rb:17:in `block in login_specific_user'

So a basic scaffolding of my controller looks as follows :

class ProfilesController < ApplicationController

  def index
    render json: Profile.where(user_id: current_user.id)
  end
end

I assume this means the user is not being logged in as I would expect

My spec is as follows

require 'spec_helper'

describe ProfilesController, type: :controller do

  before do
    @user = FactoryGirl.create :user
    @profile = FactoryGirl.create :profile, user: @user
    FactoryGirl.create :profile
  end

  describe "GET :index" do

    login_specific_user(@user)

    it "returns all profiles linked to the loged in user with the same country and client as the linked survey" do
      get :index
      expect(response.body).to eq(@profile.to_json)
    end
  end

end

My controller macro's are as follows:

    module ControllerMacros

  def login_admin
    before :each do
      sign_in ControllerMacros.get_user(@request, :admin, :admin_user)
    end
  end

  def login_user
    before :each do
      sign_in ControllerMacros.get_user(@request, :user)
    end
  end

  def login_specific_user(user)
    before :each do
      sign_in user
    end
  end

  class << self

    def get_user(req, mapping, type=mapping)
      req.env["devise.mapping"] = Devise.mappings[mapping]
      user = FactoryGirl.create(type)
      user.confirm!
      user
    end

  end
end
1

1 Answers

0
votes

I solved this by not using controller macros and just adding the following to my before block

before do
    @user = FactoryGirl.create :user
    @user.confirm!
    sign_in @user
end