【问题标题】:Rails API app: Unable to make work request RspecsRails API 应用程序:无法发出工作请求 Rspecs
【发布时间】:2023-01-13 01:18:35
【问题描述】:

控制器代码

# frozen_string_literal: true

module Api
  module V1
    class MarketplacesController < ApplicationController
      before_action :authenticate_api_v1_user!
      ...
      def index
        render json: Marketplace.all, status: :ok,
               include: 'url'
      end
      ...
    end
  end
end

第一种方法

我在帮助文件中创建了这个方法

# spec/requests_helper.rb file

def login(user)
  post api_v1_user_session_path, params: {
    email: user.email, password: user.password
  }.to_json, as: :json
end

并在我的 rspec 文件中使用它:

# spec/requests/api/v1/marketplace_spec.rb file
require "rails_helper"
require "requests_helper"

RSpec.describe Marketplace, :type => :request do
  let(:user) { create(:user) }

  context "when signed in" do
    it "returns all marketplaces" do
      login(user)
      get "/api/v1/marketplaces"

      expect(response.body).not_to include('redirected') # temporary while finding out why I get a redirect HTML
    end
  end
end

但登录方法抛出:

"{\"success\":false,\"errors\":[\"Invalid login credentials. Please try again.\"]}"

登录请求的正文是:

"\"{\\\"email\\\":\\\"test_user@test.te\\\",\\\"password\\\":\\\"password\\\"}\""

因此,正文中发送的密码似乎与创建的用户的密码相同,如该工厂中所定义:

# spec/factories/users.rb file
FactoryBot.define do
  factory :user, class: 'User' do
    name { 'A user' }
    uid { 'test_user@test.te' }
    email { 'test_user@test.te' }
    password { 'password'}
  end
end

最后,/api/v1/marketplaces 的请求抛出:

"{"errors":[\"You need to sign in or sign up before continuing.\"]}"

这是预料之中的,因为如前所示登录不成功。

第二种方法

我使用登录助手代替我的登录方法:

# spec/requests/api/v1/marketplace_spec.rb file
require "rails_helper"
require "requests_helper"

RSpec.describe Marketplace, :type => :request do
  let(:user) { create(:user) }

  context "when signed in" do
    it "returns all marketplaces" do
      sign_in user
      get "/api/v1/marketplaces"

      expect(response.body).not_to include('redirected') # temporary while finding out why I get a redirect HTML
    end
  end
end

可以通过在 spec/rails_helper.rb 文件中添加:

...
config.include Devise::Test::IntegrationHelpers, type: :request # if Rails.env.test?

但是,为此,get "/api/v1/marketplaces" 的请求响应主体突然变成了:

<html><body>You are being <a href=\"http://www.example.com/api/v1/auth/sign_in\">redirected</a>.</body></html>

这对我来说有点奇怪,因为我正在做一个 Rails API

第三种方法

与上一个相同,但这次我试图通过添加到 config/environments/test.rb 文件来更改这个“example.com”:

...
config.action_controller.default_url_options = {
  host: 'localhost:3000', # or whatever your host is
  port: '3000'
}

但是 get "/api/v1/marketplaces" 的响应正文是

<html><body>You are being <a href=\"http://localhost:3000/api/v1/auth/sign_in\">redirected</a>.</body></html>

【问题讨论】:

  • 您可以发布会话和市场的控制器代码吗?
  • @Chiperific 我编辑了帖子并在其中包含了最重要的内容。我无法发布整件事。不过,我不认为它太相关,因为没有完成的是实际的身份验证,而这与任何控制器或其中的代码无关。
  • 方法 2 和 3 感觉像是您需要更改以使其成为 API 而不是 HTML 的 Devise 设置,但我认为它仍会重定向您。方法 1 似乎应该有效。我会在 post 调用之前放置一个调试器,看看为什么这些凭据与数据库记录不匹配。

标签: rspec devise rspec-rails ruby-on-rails-6


【解决方案1】:
<html><body>You are being <a href="http://www.example.com/api/v1/auth/sign_in">redirected</a>.</body></html>
ans => this errors is rise beacuse the user is not sign_in yet.
 add before block just below to describe 
 RSpec.describe Marketplace type: :request do 
     let(:user) {create :user}
     before(:each) do 
       sign_in(:user)
     end
   describe " " do 
     it ' ' do 
       #your request
     end
   end
end
      

【讨论】:

    猜你喜欢
    • 2013-10-09
    • 1970-01-01
    • 1970-01-01
    • 2020-02-25
    • 2018-08-22
    • 2019-02-02
    • 1970-01-01
    • 2021-05-10
    • 2018-12-17
    相关资源
    最近更新 更多