From c1d0a7887beb2418200e4171642341577d3832b3 Mon Sep 17 00:00:00 2001 From: Daniel Colson Date: Fri, 12 Apr 2019 10:05:17 -0400 Subject: [PATCH] Add back loading definitions in after_initialize We removed the call to `FactoryBot.load_definitions` in the `after_initialize` hook in dcfc9f6 because the other changes in that commit were causing us to sometimes call `FactoryBot.load_definitions` after `FactoryBot.reload`. We could have changed `FactoryBot.load_definitions` to `FactoryBot.reload`, which is what this PR does, but it didn't seem necessary at the time since all the tests were still passing. We fixed a problem in 9b83f48 that was causing us to watch the root directory of the project. This fix caused a test failure, but I ended up changing the test in 2b7bca0 because it seemed like an unlikely scenario. This PR adds back the original test case. Although I do think it unlikely, I would rather not risk making a breaking change in the next patch release. --- features/load_definitions.feature | 39 ++++++++++++++++++++++++++++++- lib/factory_bot_rails/railtie.rb | 4 ++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/features/load_definitions.feature b/features/load_definitions.feature index c0c1f5f..85d5236 100644 --- a/features/load_definitions.feature +++ b/features/load_definitions.feature @@ -115,7 +115,44 @@ Feature: automatically load factory definitions When I run `bundle exec rake test` with a clean environment Then the output should contain "1 assertions, 0 failures, 0 errors" - Scenario: use 3rd-party factories with an initializer + Scenario: use 3rd-party factories with an initializer and without any user-defined factories + When I append to "config/application.rb" with: + """ + require File.expand_path('../../lib/some_railtie/railties.rb', __FILE__) + """ + When I write to "lib/some_railtie/railties.rb" with: + """ + module SomeRailtie + class Railtie < ::Rails::Engine + initializer "some_railtie.factories", :after => "factory_bot.set_factory_paths" do + FactoryBot.definition_file_paths << File.expand_path('../factories', __FILE__) + end + end + end + """ + When I write to "lib/some_railtie/factories.rb" with: + """ + FactoryBot.define do + factory :factory_from_some_railtie, class: 'User' do + name { 'Artem' } + end + end + """ + When I write to "test/unit/user_test.rb" with: + """ + require 'test_helper' + + class UserTest < ActiveSupport::TestCase + test "use factory of some_railtie" do + railtie_user = FactoryBot.create(:factory_from_some_railtie) + assert_equal 'Artem', railtie_user.name + end + end + """ + When I run `bundle exec rake test` with a clean environment + Then the output should contain "1 assertions, 0 failures, 0 errors" + + Scenario: use 3rd-party factories with an initializer together with a user-defined factory When I append to "config/application.rb" with: """ require File.expand_path('../../lib/some_railtie/railties.rb', __FILE__) diff --git a/lib/factory_bot_rails/railtie.rb b/lib/factory_bot_rails/railtie.rb index 1f948d1..2e9cd81 100644 --- a/lib/factory_bot_rails/railtie.rb +++ b/lib/factory_bot_rails/railtie.rb @@ -22,6 +22,10 @@ module FactoryBotRails Reloader.new(app, config).run end + config.after_initialize do + FactoryBot.reload + end + private def definition_file_paths