1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot_rails.git synced 2022-11-09 11:49:18 -05:00

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.
This commit is contained in:
Daniel Colson 2019-04-12 10:05:17 -04:00
parent 9b83f482b5
commit c1d0a7887b
2 changed files with 42 additions and 1 deletions

View file

@ -115,7 +115,44 @@ Feature: automatically load factory definitions
When I run `bundle exec rake test` with a clean environment When I run `bundle exec rake test` with a clean environment
Then the output should contain "1 assertions, 0 failures, 0 errors" 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: When I append to "config/application.rb" with:
""" """
require File.expand_path('../../lib/some_railtie/railties.rb', __FILE__) require File.expand_path('../../lib/some_railtie/railties.rb', __FILE__)

View file

@ -22,6 +22,10 @@ module FactoryBotRails
Reloader.new(app, config).run Reloader.new(app, config).run
end end
config.after_initialize do
FactoryBot.reload
end
private private
def definition_file_paths def definition_file_paths