Active Storage issue when uploading a file using fixture_file_upload - Rspec

1.1k Views Asked by At

I have to test my Grape API request which furthermore calls a service, this service saves the broker_profile data. When I upload an image using fixture file upload it throws an error of <ArgumentError: wrong number of arguments (given 1, expected 0; required keywords: io, filename)>, I recently migrated to active storage from paperclip, before migrating tests were working properly. Currently, rails version is 5.2.4.2 ruby version in 2.6.6

Basically the question is how I can properly test (Rspsc) my API which uses Active storage to attach an image?

Service file

class BrokerProfileService
  def initialize(broker_profile)
    @broker_profile = broker_profile
  end

  def self.create!(attrs)
    new_broker = BrokerProfile.new
    new(new_broker).update!(attrs)
    new_broker
  end

  def update!(attrs)
    @broker_profile.assign_attributes(attrs)
    @broker_profile.save!
  end

end

Rspec file

describe API::V1::BrokerProfiles do
  describe "POST /v1/broker_profiles" do
    let(:headers) { auth_header_for(profile) }

    let(:expect_brokerage_endpoint) { false }
    let(:path) { "/v1/broker_profiles" }

    let(:password) { nil }
    let(:params) { {
      broker_profile: {
        first_name: "John",
        last_name: "Doe",
        email: "[email protected]",
        password: password,
        job_title: "Consultant",
        phone: "+986455454",
        location: "Abu Dhabi",
        linkedin_url: "http://linkedin.com/111",
        code: "333",
        bio: "worked hard",
        image: fixture_file_upload("#{fixtures_dir}/images/test.jpg", "image/jpeg")
      }
    }}

    subject { post path, params: params, headers: headers }



    context "as admin" do
      let(:brokerage) { create(:brokerage) }
      let(:profile) { create(:admin_profile, brokerage: brokerage) }

      context "when params are correct" do
        def expect_created(password_set: false)
          subject
          expect(response).to be_successful

          broker_profile_payload = response_json["broker_profile"]
          expect(broker_profile_payload).to be_a(Hash)
          if expect_brokerage_endpoint
            expect(broker_profile_payload["id"]).to eq nil
            expect(broker_profile_payload["brokerage_name"]).to eq nil
          else
            expect(broker_profile_payload["id"]).to be_an(Integer)
            expect(broker_profile_payload["brokerage_name"]).to eq profile.brokerage.name
          end
          expect(broker_profile_payload["code"]).to eq "333"
          expect(broker_profile_payload["first_name"]).to eq "John"
          expect(broker_profile_payload["last_name"]).to eq "Doe"
          expect(broker_profile_payload["email"]).to eq "[email protected]"
          expect(broker_profile_payload["is_password_set"]).to eq password_set
          expect(broker_profile_payload["job_title"]).to eq "Consultant"
          expect(broker_profile_payload["phone"]).to eq "+971503798758"
          expect(broker_profile_payload["location"]).to eq "Abu Dhabi"
          expect(broker_profile_payload["linkedin_url"]).to eq "http://linkedin.com/111"
          expect(broker_profile_payload["bio"]).to eq "worked hard"

          broker_profile = brokerage.broker_profiles.find_by_code(broker_profile_payload["code"])
          expect(broker_profile.manually_created).to eq true

          broker_profile
        end



        context "and password is not set" do
          specify do
            broker_profile = expect_created
            expect(broker_profile.user.valid_password?(nil)).to eq false
          end
        end

      end
  end
end
1

There are 1 best solutions below

0
alex On

Interesting. I've had success with the following test.

RSpec.describe BrokersController, type: :controller do
  render_views

  before do
    post :create, params: { broker: { name: "Test", image: fixture_file_upload("test-image.jpg") } }, format: :json
  end

  it "updates the broker successfully" do
    parsed_response = JSON.parse(response.body)

    expect(response).to be_successful
    expect(parsed_response["image"]).to include("/rails/active_storage/blobs/")
    expect(parsed_response["image"]).to include("test-image.jpg")
    expect(parsed_response.keys).to eq(["name", "image"])
  end
end

And for reference, that test-image.jpg is stored in spec/fixtures/test-image.jpg.