How to assign global variable inside Rswag 2.3.2 spec tests?

441 Views Asked by At

I'm new to RoR, a real newbie. But I got a project that need RoR for the backend development. And in this project for the API documentation is using Rswag. and I hit a wall when tried to authorizing the endpoints using JWT, which I want to create the user and the JWT token on the global, similar to what Rspec can do.

But I got a problem when trying to assign a global variable into the rswag tests, I already tried using the before(:each), let() and let!() as well, but they still didn't work which working on the 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

And here is the error that I got when I tried using let() or let!()

enter image description here

and if I tried to use the Before Block, it doesn't return any errors, but it's also didn't get triggered when I put the byebug in the before block.

currently here are the installed version lists:

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

And here is the closest answer I can found in the issues, but this doesn't resolve the problem I'm facing now

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

Any bits of help is appreciated, thank you :smile:

0

There are 0 best solutions below