ruby-on-railstestingrspecswaggerrswag

test returning pdf with swagger in ruby on rails


I'm trying to test an endpoint that returns a pdf file.

My action looks like this:

    def pdf
     respond_to do |format|
      format.pdf do
        @pdf = ApplicationController.render pdf: some_number,
                                template: "show",
                                layout: nil
        send_data(@pdf, :filename => "something.pdf", :type => "application/pdf", disposition: 'attachment')
      end
     end
   end

and my test code looks like this:

  path "/pdf" do
    get "return PDF" do
      produces 'application/pdf'

      response '200', 'PDF generated' do
        schema type: :file

        before do |example|
          sign_in @some_user
          submit_request(example.metadata)
        end

        it 'returns a 200 response' do |example|
          assert_response_matches_metadata(example.metadata)
        end
      end
    end
  end

but I'm getting the error: ActionController::UnknownFormat

I'm using: ruby 3.1.0, rails 7.0.0, rswag 2.8.0, openapi 3.0.1

Can anyone help me with my issue? and what is the correct way to test returning pdf?


Solution

  • Simply I solved the issue by adding .pdf to the URL:

    path "/pdf.pdf" do
       ...
    end