thoughtbot / thoughtbot/factory_bot

Improve experience with file uploads

Open
#1,282 16 comments 17 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
Ruby
Stars
8.2k
Forks
2.6k
PR merge metrics
No merged PRs in 30d

Description

Related: https://github.com/thoughtbot/factory_bot/issues/385

On a current project we have a trait that looks like this:

    trait :with_profile_image do
      transient do
        profile_image_file { Rails.root.join("spec", "fixtures", "jpeg.jpg") }
      end

      after :build do |person, evaluator|
        file = evaluator.profile_image_file

        person.profile_image.attach(
          io: file.open,
          filename: file.basename.to_s,
        )
      end
    end

This seems overly complicated. Maybe we could have done something more like:

    trait :with_profile_image do
      profile_image do
        Rack::Test::UploadedFile.new('spec/fixtures/jpeg.jpg', 'image/jpeg')
      end
    end

But that also seems a little awkward to me, and something I am unlikely to remember.

I would like to at least see some documentation for the best way to handle file uploads in factory_bot, and maybe we could build something into factory_bot or factory_bot_rails to make this easier.

Maybe something like:

    trait :with_profile_image do
      profile_image { file_fixture("jpeg.jpg") }
    end

See https://github.com/rails/rails/blob/b9ca94caea2ca6a6cc09abaffaad67b447134079/activesupport/lib/active_support/testing/file_fixtures.rb#L24, https://github.com/rails/rails/blob/61c4be477706b721688e36a3168f86aabc625658/activestorage/test/test_helper.rb#L82-L84, and https://github.com/rack-test/rack-test/blob/a2f762d5abc2ead277dab51794d22083b72809ea/lib/rack/test/uploaded_file.rb for inspiration

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the issue discussion and the referenced Rails files, especially active_support/testing/file_fixtures.rb and activestorage/test/test_helper.rb, along with rack/test/uploaded_file.rb. Determine whether the accepted scope is documentation or a new factory_bot/factory_bot_rails helper; done means the supported recommendation and usage are clearly documented, or the agreed helper behavior is implemented and documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
rails, ruby
Domain
testing
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.