Skip to content

Add file_fixture_upload to factories #547

@jmschp

Description

@jmschp

Problem this feature will solve

When defining factories that involve file uploads (e.g., Active Storage attachments or models with has_one_attached attributes), it’s convenient to use Rails’ built-in helpers for test files.

factory_bot_rails already makes the file_fixture helper available in factories, but not file_fixture_upload, which is its natural companion in Rails tests. This inconsistency makes it harder to use Rails’ native testing tools for simulating file uploads inside factories.

Desired solution

Include the file_fixture_upload helper in the methods available to FactoryBot factories (similar to how file_fixture is currently included).

This would allow factories to use upload helpers like:

factory :user do
  avatar { file_fixture_upload('avatar.png', 'image/png') }
end

without requiring additional setup in the test suite.

Alternatives considered

  • Manually including ActionDispatch::TestProcess into FactoryBot::SyntaxRunner, e.g.:

    FactoryBot::SyntaxRunner.include ActionDispatch::TestProcess::FixtureFile

    While functional, this workaround feels inconsistent with the built-in inclusion of file_fixture and requires additional setup in every Rails project using factory_bot_rails.

Additional context

file_fixture_upload is a standard Rails helper that complements file_fixture, both of which are part of Rails’ test support layer. Since factory_bot_rails is already Rails-specific and includes similar helpers, adding this one would improve consistency and developer experience with minimal additional coupling.

I am more than happy to start a PR if you consider this useful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions