file_fixture_upload always passes File object as string in rspec rails 6
Asked Answered
H

1

6

I am trying to upload a file in rspec and use that file in my controller. Everything worked fine in Rails 4 but now in Rails 6, the Rack::Test::UploadedFile hash when received in controller, my tempfile filed is a File hash string object which makes it impossible to be used.

describe V1::ApplicationsApiController, type: :request do
  let(:ic_front) { fixture_file_upload(file_fixture("ic_front.jpg")) }
  let(:ic_back) { fixture_file_upload(file_fixture("ic_back.png")) }

  context "Loan Eligibility Check Screening" do
    it "runs background check v1.0.1" do
      payload = {
        "ic_front" => ic_front,
        "ic_back_path" => ic_back,
        "data" => {
          "product_code" => product.code,
          "include" => ["fico"],
          "applicant" => {
            "name" => "Florian Test 1",
            "ic_or_passport_no" => "1234567890",
            "consented_at" => Time.now.iso8601,
          }
        }
      }

      VCR.use_cassette('ic_check_and_ctos') do
        post "/api/v1.0.1/applications/background_check", params: payload, as: :json
        expect(response.status).to eq 200
      end
    end
  end
end

Now in my controller, i get the following in params[:ic_front]

<ActionController::Parameters {"original_filename"=>"ic_front.jpg", "tempfile"=>"#<File:0x0000558457ecd5c0>", "content_type"=>nil} permitted: false>

In my controller, i want to be able to get the params[:ic_front].tempfile.path which i cannot do because tempfile is a string file hash.

I have added

include ActionDispatch::TestProcess
include Rack::Test::Methods

in my rails_helper.rb but didnt work. I tried moving into my tests but also didn't work as well.

Appreciate any new input on this

Hodgkins answered 22/1, 2020 at 4:39 Comment(1)
Remove as: :json from the post call. If you need the response to be formatted as json, that should be specified as part of the url (e.g. background_check.json).Chuckhole
H
0
post "/api/v1.0.1/applications/background_check", params: payload, as: :json

The problem is that you can't send files using json (as: :json). Files need to be sent as part of a multipart/form-data request so the server can process the file correctly.

Hesperides answered 12/8 at 21:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.