微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 Rswag 2.3.2 规范测试中分配全局变量?

如何解决如何在 Rswag 2.3.2 规范测试中分配全局变量?

我是 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,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,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!()

时遇到的错误

enter image description here

如果我尝试使用 Before Block,它不会返回任何错误,但是当我将 byebug 放入 before 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

感谢任何帮助,谢谢:smile:

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?