diff --git a/lib/factory_bot_rails/railtie.rb b/lib/factory_bot_rails/railtie.rb index 2e9cd81..c8669a7 100644 --- a/lib/factory_bot_rails/railtie.rb +++ b/lib/factory_bot_rails/railtie.rb @@ -18,12 +18,8 @@ module FactoryBotRails FactoryBot.definition_file_paths = definition_file_paths end - initializer "factory_bot.register_reloader" do |app| - Reloader.new(app, config).run - end - - config.after_initialize do - FactoryBot.reload + config.after_initialize do |app| + Reloader.new(app).run end private diff --git a/lib/factory_bot_rails/reloader.rb b/lib/factory_bot_rails/reloader.rb index f3d66fd..e54fde1 100644 --- a/lib/factory_bot_rails/reloader.rb +++ b/lib/factory_bot_rails/reloader.rb @@ -4,21 +4,22 @@ require "factory_bot_rails/definition_file_paths" module FactoryBotRails class Reloader - def initialize(app, config) + def initialize(app) @app = app - @config = config @paths = DefinitionFilePaths.new(FactoryBot.definition_file_paths) end def run return unless @paths.any? - register_reloader(build_reloader) + reloader = build_reloader + register_reloader(reloader) + reloader.execute end private - attr_reader :app, :config + attr_reader :app def build_reloader reloader_class.new(@paths.files, @paths.directories) do @@ -31,11 +32,11 @@ module FactoryBotRails end def register_reloader(reloader) - config.to_prepare do + app.reloaders << reloader + + app.reloader.to_prepare do reloader.execute end - - app.reloaders << reloader end end end diff --git a/spec/factory_bot_rails/reloader_spec.rb b/spec/factory_bot_rails/reloader_spec.rb index 188e791..d66d217 100644 --- a/spec/factory_bot_rails/reloader_spec.rb +++ b/spec/factory_bot_rails/reloader_spec.rb @@ -12,8 +12,6 @@ describe FactoryBotRails::Reloader do context "when a definition file paths exist" do it "registers a reloader" do - allow(reloader_class).to receive(:new) - run_reloader(["spec/fixtures/factories", "not_exist_directory"]) expect(reloader_class).to have_received(:new) @@ -22,8 +20,6 @@ describe FactoryBotRails::Reloader do context "when a file exists but not a directory" do it "registers a reloader" do - allow(reloader_class).to receive(:new) - run_reloader(["spec/fake_app", "not_exist_directory"]) expect(reloader_class).to have_received(:new) @@ -32,8 +28,6 @@ describe FactoryBotRails::Reloader do context "when a definition file paths NOT exist" do it "does NOT register a reloader" do - allow(reloader_class).to receive(:new) - run_reloader(["not_exist_directory"]) expect(reloader_class).not_to have_received(:new) @@ -42,12 +36,24 @@ describe FactoryBotRails::Reloader do def run_reloader(definition_file_paths) FactoryBot.definition_file_paths = definition_file_paths - FactoryBotRails::Reloader. - new(Rails.application, Rails.application.config).run + FactoryBotRails::Reloader.new(app).run + end + + def app + double( + :app, + config: double(:config, file_watcher: reloader_class), + reloader: double(:reloader, to_prepare: nil), + reloaders: [], + ) end def reloader_class - Rails.application.config.file_watcher + @reloader_class ||= + double( + :reloader_class, + new: double(:reloader, execute: nil), + ) end end end