ActiveStorage::IntegrityError Rspec attaching file

Viewed 1260

I have a factory for create dummy data, the factory name is migration_session.

So i create one instance of that factory by calling like this

@loan_migration = create(:migration_session, :loan, cooperative: @cooperative, management: @manager)

and this is my migration_session factory code

FactoryBot.define do
  factory :migration_session, class: Migration::Session do
    cooperative
    management
    trait :loan do
      excel_file { Rack::Test::UploadedFile.new("#{Rails.root}/public/payment_migration.xlsx", 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') }
    end

this is my migration_session model

class Migration::Session < ApplicationRecord
  self.table_name = "migration_sessions"

  has_one_attached :excel_file
end

because i call trait loan, it will attached excel_file attributes by excel file payment_migration.xlsx. but error like this comes after try to save the instance

 ActiveStorage::IntegrityError:
       ActiveStorage::IntegrityError
     # /usr/local/bundle/gems/activestorage-6.0.3.4/lib/active_storage/service/disk_service.rb:154:in `ensure_integrity_of'
     # /usr/local/bundle/gems/activestorage-6.0.3.4/lib/active_storage/service/disk_service.rb:21:in `block in upload'
     # /usr/local/bundle/gems/activestorage-6.0.3.4/lib/active_storage/service.rb:126:in `instrument'
     # /usr/local/bundle/gems/activestorage-6.0.3.4/lib/active_storage/service/disk_service.rb:19:in `upload'
     # /usr/local/bundle/gems/activestorage-6.0.3.4/app/models/active_storage/blob.rb:196:in `upload_without_unfurling'
     # /usr/local/bundle/gems/activestorage-6.0.3.4/lib/active_storage/attached/changes/create_one.rb:25:in `upload'
     # /usr/local/bundle/gems/activestorage-6.0.3.4/lib/active_storage/attached/model.rb:56:in `block in has_one_attached'
     # ./spec/requests/bulk_uploads_spec.rb:36:in `block (2 levels) in <top (required)>'
     # /usr/local/bundle/gems/webmock-3.9.5/lib/webmock/rspec.rb:37:in `block (2 levels) in <top (required)>'

How to solve this problem? My file just fine, i can open and edit it.

1 Answers

I thing your problem is in the file path

excel_file { Rack::Test::UploadedFile.new("#{Rails.root}/public/payment_migration.xlsx", 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') }

Depending on your SO, the '/' char may not be allowed on path, so instead of pass the path string, let rails fill it for you.

Use the Rails.root.join method, like this: Rails.root.join('public', 'payment_migration.xlsx')

Related