SsPay
SsPay

Reputation: 191

Validate for file format doesn't pass during tests

I have model ImportFile and it has has_one_attached :document and validate for file format: validates :document, blob: { content_type: ['application/vnd.ms-excel'] } (only .xls)

I created factory for this model:

FactoryBot.define do
  factory :import_file do
    association :user
    processed { false }
    document { Rack::Test::UploadedFile.new('spec/fixtures/orders.xls') }
  end
end

And when i run tests, they didn't pass, i got validation error:

Validation failed: Document is not a valid file format

But if i load this file on site it works perfectly.

Why format validation doesn't pass during tests?

Upvotes: 0

Views: 73

Answers (1)

max
max

Reputation: 102001

FactoryBot.define do
  factory :import_file do
    association :user
    processed { false }
    document do 
       Rack::Test::UploadedFile.new(
         'spec/fixtures/orders.xls', 
         content_type: 'application/vnd.ms-excel'
       )
    end
  end
end

Rack::Test::UploadedFile does not actually try to deduce the mime type from the file - it just defaults to 'text/plain'.

Upvotes: 1

Related Questions