0

我是 RoR 的新手,一个真正的新手。但是我有一个需要 RoR 进行后端开发的项目。在这个 API 文档的项目中使用的是Rswag. 当我尝试使用 授权端点时我碰壁了JWT,我想在全局上创建用户和 JWT 令牌,类似于Rspec可以做的事情。

但是在尝试将全局变量分配给rswag测试时遇到了问题,我已经尝试过使用before(:each),let()以及let!(),但它们仍然无法正常工作Rspec

require "swagger_helper"

RSpec.describe "Profile API", type: :request do
  # I also tried to assign the variables before each test cases
  #
  # let(:user) { FactoryBot.create(:user, role_id: 3, phone_number: "+6285123456799") }
  # --- OR ---
  # before(:each) do
  #   @user = FactoryBot.create(:user, role_id: 3, phone_number: "+6285123456799")
  # end

  path "/api/v1/profiles" do
    post "Create new profile" do
      tags "Profile"
      consumes "application/json"
      produces "application/json"
      security [ Bearer: {} ]
      parameter name: :profile, in: :body, schema: {
        type: :object,
        properties: {
          first_name: {type: :string},
          last_name: {type: :string},
        },
        required: [:first_name]
      }

      # I've tried put it here so all the test blocks can access it
      # let!(:user) { FactoryBot.create(:user, role_id: 3, phone_number: "+6285123456789") }


      response 201, "All fields filled" do
        let(:"Authorization") { "Bearer #{auth_header(@user)}" }
        let(:profile) { {first_name: "John", last_name: "Doe"} }
        
        run_test! do |response|
          expect(response_body).to eq({
            "first_name" => "John",
            "last_name" => "Doe"
          })
        end
      end
   end
end

这是我尝试使用let()或时遇到的错误let!()

在此处输入图像描述

如果我尝试使用Before Block,它不会返回任何错误,但是当我byebugbefore block.

目前这里是安装的版本列表:

Ruby version 3.0.0
Rails version 6.1.1
Rspec-rails gem version 4.0.2
Rswag gem version 2.3.2

这是我在问题中找到的最接近的答案,但这并不能解决我现在面临的问题

https://github.com/rswag/rswag/issues/168#issuecomment-454797784

感谢您提供任何帮助,谢谢:微笑:

4

0 回答 0