Factory_bot: Associations not properly mapping file_fixture_upload properties

Created on 14 Jan 2021  路  1Comment  路  Source: thoughtbot/factory_bot

Description

Once I upgrade to macOS Big Sur my specs started failing and I've tracked it down to fixture_file_upload, but only when there's an association involved.

FactoryBot.define do
  factory :custom_field do
    name 'custom_field'
    field_type 'text_field' # potential options are all the file names in views/custom_fields/*
    required false
    association :test_report_form
    model 'Test'
    element_list ''
    display_text false
  end
end

FactoryBot.define do
  factory :test_report_form do
    name { Faker::Lorem.sentence(2) }
    report_layout { fixture_file_upload "#{Rails.root}/spec/factories/paperclip/wm_test_report.tlf", 'text/plain' }
  end
end

# Custom field spec
let!(:custom_field) { create(:custom_field, name: 'Contact Person') }

Stack Trace:

ActiveRecord::RecordInvalid:
       Validation failed: Report content type is invalid, Report is invalid
     factory_bot-4.8.2/lib/factory_bot/configuration.rb:18:in `block in initialize'
     factory_bot-4.8.2/lib/factory_bot/evaluation.rb:18:in `create'
     factory_bot-4.8.2/lib/factory_bot/strategy/create.rb:12:in `block in result'
     factory_bot-4.8.2/lib/factory_bot/strategy/create.rb:9:in `tap'
     factory_bot-4.8.2/lib/factory_bot/strategy/create.rb:9:in `result'
     factory_bot-4.8.2/lib/factory_bot/factory.rb:43:in `run'
     factory_bot-4.8.2/lib/factory_bot/factory_runner.rb:29:in `block in run'
     factory_bot-4.8.2/lib/factory_bot/factory_runner.rb:28:in `run'
     factory_bot-4.8.2/lib/factory_bot/strategy/create.rb:5:in `association'
     factory_bot-4.8.2/lib/factory_bot/evaluator.rb:33:in `association'
     factory_bot-4.8.2/lib/factory_bot/attribute/association.rb:19:in `block in to_proc'
     factory_bot-4.8.2/lib/factory_bot/evaluator.rb:77:in `instance_exec'
     factory_bot-4.8.2/lib/factory_bot/evaluator.rb:77:in `block in define_attribute'
     factory_bot-4.8.2/lib/factory_bot/attribute_assigner.rb:56:in `get'
     factory_bot-4.8.2/lib/factory_bot/attribute_assigner.rb:16:in `block (2 levels) in object'
     factory_bot-4.8.2/lib/factory_bot/attribute_assigner.rb:15:in `each'
     factory_bot-4.8.2/lib/factory_bot/attribute_assigner.rb:15:in `block in object'
     factory_bot-4.8.2/lib/factory_bot/attribute_assigner.rb:14:in `tap'
     factory_bot-4.8.2/lib/factory_bot/attribute_assigner.rb:14:in `object'
     factory_bot-4.8.2/lib/factory_bot/evaluation.rb:13:in `object'
     factory_bot-4.8.2/lib/factory_bot/strategy/create.rb:9:in `result'
     factory_bot-4.8.2/lib/factory_bot/factory.rb:43:in `run'
     factory_bot-4.8.2/lib/factory_bot/factory_runner.rb:29:in `block in run'
     factory_bot-4.8.2/lib/factory_bot/factory_runner.rb:28:in `run'
     factory_bot-4.8.2/lib/factory_bot/strategy/create.rb:5:in `association'
     factory_bot-4.8.2/lib/factory_bot/evaluator.rb:33:in `association'
     factory_bot-4.8.2/lib/factory_bot/attribute/association.rb:19:in `block in to_proc'
     factory_bot-4.8.2/lib/factory_bot/evaluator.rb:77:in `instance_exec'
     factory_bot-4.8.2/lib/factory_bot/evaluator.rb:77:in `block in define_attribute'
     factory_bot-4.8.2/lib/factory_bot/attribute_assigner.rb:56:in `get'
     factory_bot-4.8.2/lib/factory_bot/attribute_assigner.rb:16:in `block (2 levels) in object'
     factory_bot-4.8.2/lib/factory_bot/attribute_assigner.rb:15:in `each'
     factory_bot-4.8.2/lib/factory_bot/attribute_assigner.rb:15:in `block in object'
     factory_bot-4.8.2/lib/factory_bot/attribute_assigner.rb:14:in `tap'
     factory_bot-4.8.2/lib/factory_bot/attribute_assigner.rb:14:in `object'
     factory_bot-4.8.2/lib/factory_bot/evaluation.rb:13:in `object'
     factory_bot-4.8.2/lib/factory_bot/strategy/create.rb:9:in `result'
     factory_bot-4.8.2/lib/factory_bot/factory.rb:43:in `run'
     factory_bot-4.8.2/lib/factory_bot/factory_runner.rb:29:in `block in run'
     factory_bot-4.8.2/lib/factory_bot/factory_runner.rb:28:in `run'
     factory_bot-4.8.2/lib/factory_bot/strategy/create.rb:5:in `association'
     factory_bot-4.8.2/lib/factory_bot/evaluator.rb:33:in `association'
     factory_bot-4.8.2/lib/factory_bot/attribute/association.rb:19:in `block in to_proc'
     factory_bot-4.8.2/lib/factory_bot/evaluator.rb:77:in `instance_exec'
     factory_bot-4.8.2/lib/factory_bot/evaluator.rb:77:in `block in define_attribute'
     factory_bot-4.8.2/lib/factory_bot/attribute_assigner.rb:56:in `get'
     factory_bot-4.8.2/lib/factory_bot/attribute_assigner.rb:16:in `block (2 levels) in object'
     factory_bot-4.8.2/lib/factory_bot/attribute_assigner.rb:15:in `each'
     factory_bot-4.8.2/lib/factory_bot/attribute_assigner.rb:15:in `block in object'
     factory_bot-4.8.2/lib/factory_bot/attribute_assigner.rb:14:in `tap'
     factory_bot-4.8.2/lib/factory_bot/attribute_assigner.rb:14:in `object'
     factory_bot-4.8.2/lib/factory_bot/evaluation.rb:13:in `object'
     factory_bot-4.8.2/lib/factory_bot/strategy/create.rb:9:in `result'
     factory_bot-4.8.2/lib/factory_bot/factory.rb:43:in `run'
     factory_bot-4.8.2/lib/factory_bot/factory_runner.rb:29:in `block in run'
     factory_bot-4.8.2/lib/factory_bot/factory_runner.rb:28:in `run'
     factory_bot-4.8.2/lib/factory_bot/strategy_syntax_method_registrar.rb:20:in `block in define_singular_strategy_method'
     ./spec/models/custom_field_spec.rb:8:in `block (2 levels) in <top (required)>'  

Reproduction Steps

I'm currently working on reproducing, but would love any amount of insight to get started. I'm trying to debug through the factory_bot code as we speak. I'm worried that we are somehow overriding a factory_bot setting.

We do have a custom find_or_create piece written, but commenting it out does not effect this case.

In another spec I was able to do this, and it worked fine, which leads to believe the association is actually the issue.

test = create(:test)
test.report = fixture_file_upload "#{Rails.root}/spec/factories/paperclip/test_report.pdf", 'application/pdf'

Expected behavior

For the file details to be uploaded on all operating systems

Actual behavior

Does not work on Big Sur

System configuration

factory_bot version: 4.8.2
rails version: 5.2.2
ruby version: 2.5.3

bug

Most helpful comment

This was neither a paperclip issue, nor a factorybot issue. My example code is incorrect in that we are using a .tlf file, not a pdf. Our model then validates this: validates_attachment_content_type :report_layout, content_type: 'text/plain'

On Big Sur mac began reading this as json, instead of plain text

file -b --mime my_file.tlf

# macOS 11.1
application/json; charset=us-ascii

# macOS 10.15.6
text/plain; charset=us-ascii

# Ubuntu 18.04
text/plain; charset=us-ascii

>All comments

This was neither a paperclip issue, nor a factorybot issue. My example code is incorrect in that we are using a .tlf file, not a pdf. Our model then validates this: validates_attachment_content_type :report_layout, content_type: 'text/plain'

On Big Sur mac began reading this as json, instead of plain text

file -b --mime my_file.tlf

# macOS 11.1
application/json; charset=us-ascii

# macOS 10.15.6
text/plain; charset=us-ascii

# Ubuntu 18.04
text/plain; charset=us-ascii
Was this page helpful?
0 / 5 - 0 ratings