-
-
Notifications
You must be signed in to change notification settings - Fork 380
Description
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') }
endwithout requiring additional setup in the test suite.
Alternatives considered
-
Manually including
ActionDispatch::TestProcessintoFactoryBot::SyntaxRunner, e.g.:FactoryBot::SyntaxRunner.include ActionDispatch::TestProcess::FixtureFile
While functional, this workaround feels inconsistent with the built-in inclusion of
file_fixtureand requires additional setup in every Rails project usingfactory_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.