From 8a739aa7b0c218b01c7a8bd6b5b8eaab7f4cd2ad Mon Sep 17 00:00:00 2001 From: Sean Doyle Date: Mon, 25 Sep 2023 11:24:10 -0400 Subject: [PATCH] Support `file_fixture` in Factory definitions Related to [factory_bot#1282][] [rails/rails#45606][] has been merged and is likely to be released as part of Rails 7.1. With that addition, the path toward resolving [factory_bot#1282][] becomes more clear. If factories can pass along [Pathname][] instances to attachment attributes, Active Support will handle the rest. Instances of `ActiveSupport::TestCase` provide a [file_fixture][] helper to construct a `Pathname` instance based on the path defined by `ActiveSupport::TestCase.file_fixture_path` (relative to the Rails root directory). [factory_bot#1282]: https://github.com/thoughtbot/factory_bot/issues/1282#issuecomment-1733796049 [rails/rails#45606]: https://github.com/rails/rails/pull/45606 [Pathname]: https://docs.ruby-lang.org/en/master/Pathname.html [file_fixture]: https://api.rubyonrails.org/classes/ActiveSupport/Testing/FileFixtures.html#method-i-file_fixture --- lib/factory_bot_rails/railtie.rb | 17 +++++++++++++++++ spec/factory_bot_rails/factory_spec.rb | 19 +++++++++++++++++++ spec/fixtures/files/file.txt | 0 3 files changed, 36 insertions(+) create mode 100644 spec/factory_bot_rails/factory_spec.rb create mode 100644 spec/fixtures/files/file.txt diff --git a/lib/factory_bot_rails/railtie.rb b/lib/factory_bot_rails/railtie.rb index 5512f239..d7393c54 100644 --- a/lib/factory_bot_rails/railtie.rb +++ b/lib/factory_bot_rails/railtie.rb @@ -5,6 +5,7 @@ require "factory_bot_rails/reloader" require "factory_bot_rails/factory_validator" require "rails" +require "active_support/testing/file_fixtures" module FactoryBotRails class Railtie < Rails::Railtie @@ -31,6 +32,22 @@ class Railtie < Rails::Railtie end end + config.after_initialize do + FactoryBot::SyntaxRunner.include ActiveSupport::Testing::FileFixtures + + ActiveSupport.on_load :active_support_test_case do + FactoryBot::SyntaxRunner.file_fixture_path = file_fixture_path + end + + if defined?(RSpec) + RSpec.configure do |config| + if config.respond_to?(:file_fixture_path) + FactoryBot::SyntaxRunner.file_fixture_path = config.file_fixture_path + end + end + end + end + config.after_initialize do |app| FactoryBot.find_definitions Reloader.new(app).run diff --git a/spec/factory_bot_rails/factory_spec.rb b/spec/factory_bot_rails/factory_spec.rb new file mode 100644 index 00000000..fd167dfa --- /dev/null +++ b/spec/factory_bot_rails/factory_spec.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +describe "factory extensions" do + describe "#file_fixture" do + it "delegates to the test harness" do + define_model "Upload", filename: :string + + FactoryBot.define do + factory :upload do + filename { file_fixture("file.txt") } + end + end + + upload = FactoryBot.build(:upload) + + expect(upload.filename).to eq(file_fixture("file.txt").to_s) + end + end +end diff --git a/spec/fixtures/files/file.txt b/spec/fixtures/files/file.txt new file mode 100644 index 00000000..e69de29b