如何解决如何在 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!()
如果我尝试使用 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 举报,一经查实,本站将立刻删除。