Humayun Naseer
Humayun Naseer

Reputation: 460

Rspec with Devise in request specs

I am writing request specs in which I have to authenticate a devise user.

I have rails = 5.2 & ruby 2.6.1

I followed the instructions given Here and use simple approach.

In added the line below in my spec/rails_helper.rb

config.include Devise::Test::IntegrationHelpers, type: :request

spec/requests/securities_spec.rb

require 'rails_helper'

describe 'Security APIs', type: :request do

  let!(:security) {FactoryBot.create(:security)}
  let!(:research) {FactoryBot.create(:research, name: Faker::Company.bs, security_type_id: security.security_type_id)}
  let!(:research_result) {FactoryBot.create(:research_result, name: Faker::Company.bs, sub_category_total: Faker::Number.number(digits: 2), securities_total: Faker::Number.number(digits: 2), research_id: research.id )}
  let!(:research_active) {FactoryBot.create(:research_active, security_type_id: security.security_type_id)}

  describe 'Get /securitiees' do
    it 'will return securites' do
      @user = FactoryBot.create(:user)
      sign_in @user
      get "/securities"
      expect(response).to have_http_status(:success)
      expect(JSON.parse(response.body).size).to eq(2)
    end
  end
end

But when I run sign_in @user I get

[#Proc:0x00005630eaf015d8@/home/humayun/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/warden-1.2.9/lib/warden/test/helpers.rb:19]

I check the above response by adding a debugger before sign_in.

in my securities_controller.rb I'm authenticating the user by

before_action :authenticate_user!

I don't know why this is not working any help will be appreciated.

Upvotes: 2

Views: 1369

Answers (1)

Sean Ferney
Sean Ferney

Reputation: 98

I had a similar issue if not the same issue.

I had been trying to use this:

config.include Devise::Test::ControllerHelpers, type: :request

It worked when i changed it to this:

config.include Devise::Test::IntegrationHelpers, type: :request

I believe request specs count as integration tests. in the source it says:

...Do not use Devise::Test::ControllerHelpers in integration tests... devise...controller_helpers.rb

devise#integration-tests

Upvotes: 2

Related Questions