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.
Problem this feature will solve
When defining factories that involve file uploads (e.g., Active Storage attachments or models with
has_one_attachedattributes), it’s convenient to use Rails’ built-in helpers for test files.factory_bot_railsalready makes thefile_fixturehelper available in factories, but notfile_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_uploadhelper in the methods available to FactoryBot factories (similar to howfile_fixtureis currently included).This would allow factories to use upload helpers like:
without requiring additional setup in the test suite.
Alternatives considered
Manually including
ActionDispatch::TestProcessintoFactoryBot::SyntaxRunner, e.g.: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_uploadis a standard Rails helper that complementsfile_fixture, both of which are part of Rails’ test support layer. Sincefactory_bot_railsis 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.