Testing controllers with rspec, authenticated with Sorcery

We are using the Sorcery gem for authentication in our Rails 4 app. We want to write controller specs for our app (we are testing with Rspec), but all our methods calls in our specs are redirected as we haven't logged in to our application when running the tests.

What extra configuration changes do we need to make to allow controller specs?

rails_helper.rb

Spec.configure do |config|
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true
  config.infer_base_class_for_anonymous_controllers = false
  config.include FactoryGirl::Syntax::Methods
  config.include Sorcery::TestHelpers::Rails
end

customers_vehicles_spec.rb

require 'rails_helper'

RSpec.describe CustomersVehiclesController, type: :controller do

  describe 'POST update_customer' do
    before(:each) do
    @job = create(:job)
    @vehicle = @job.vehicle
    @customer = @vehicle.customers.last
    @user = create(:user_schade)
    @now = DateTime.now

    login_user
end

context 'user has unassinged time' do
  it 'gets the vehicle information page' do
    get :show_customer, id: @customer.id

    expect(response).to be_success
    expect(action).to render_template(partial:'customer_form')
  end
end
end

This test fails on the get; we receive a 302 (redirect) because we are not logged in.

What configuration options are we missing be able to test our controllers?

Note: We are using Sorcery 0.8.4

Upvotes: 0

Views: 626

Answers (1)

strivedi183
strivedi183

Reputation: 4831

This is something I ran into. One thing that might be worth checking is what version of the sorcery gem you are using because if the version you are using is >= 0.8.6 (0.8.6 was just released a few weeks ago I think), then you would need to replace in your rails_helper.rb

config.include Sorcery::TestHelpers::Rails

with

config.include Sorcery::TestHelpers::Rails::Controller, type: :controller
config.include Sorcery::TestHelpers::Rails::Integration, type: :feature

as shown in upgrading which has more information.

Hope this helps!

Upvotes: 1

Related Questions