简体   繁体   English

Rspec 与 Devise 在请求规格

[英]Rspec with Devise in request specs

I am writing request specs in which I have to authenticate a devise user.我正在编写请求规范,其中我必须验证 devise 用户。

I have rails = 5.2 & ruby 2.6.1我有导轨 = 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在我的 spec/rails_helper.rb 中添加了以下行

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

spec/requests/securities_spec.rb规范/请求/security_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但是当我运行 sign_in @user 我得到

[#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] [#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.我通过在 sign_in 之前添加调试器来检查上述响应。

in my securities_controller.rb I'm authenticating the user by在我的Securities_controller.rb 中,我通过以下方式对用户进行身份验证

before_action:authenticate_user! before_action:authenticate_user!

I don't know why this is not working any help will be appreciated.我不知道为什么这不起作用任何帮助将不胜感激。

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::Test::ControllerHelpers ... devise...controller_helpers.rb

devise#integration-tests设计#集成测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM