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 indcfc9f6because 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 in9b83f48that was causing us to watch the root directory of the project. This fix caused a test failure, but I ended up changing the test in2b7bca0because 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:
		
							parent
							
								
									9b83f482b5
								
							
						
					
					
						commit
						c1d0a7887b
					
				
					 2 changed files with 42 additions and 1 deletions
				
			
		| 
						 | 
				
			
			@ -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__)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -22,6 +22,10 @@ module FactoryBotRails
 | 
			
		|||
      Reloader.new(app, config).run
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    config.after_initialize do
 | 
			
		||||
      FactoryBot.reload
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    private
 | 
			
		||||
 | 
			
		||||
    def definition_file_paths
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue