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