Skip to content

Commit 1b80bc9

Browse files
committed
Reload after changing file paths to play nice with FactoryBotRails
FactoryBotRails calls find_definitions in it's railtie as rails initializes and we can't assume we're the first one to call. Instead we call reload, which will reset things and then call find_definitions. find_definitions isn't reentrant so if it's already run, a second call will try to register the same factories from the same files and lead to the following error: ``` FactoryBot::DuplicateDefinitionError: Factory already registered: account ```
1 parent 1da4461 commit 1b80bc9

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

spec/support/factory_bot_helper.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ def seq_padded_for_sorting(n)
2727
FactoryBot.definition_file_paths << File.join(engine.root, 'spec', 'factories')
2828
end
2929

30-
FactoryBot.find_definitions
30+
# find_definitions is not reentrant, so call reload to reset things before it calls find_definitions.
31+
# Other code, such as factory bot rails, may have already registered factories, so we can't assume we're the first to call
32+
# find_definitions.
33+
FactoryBot.reload
3134

3235
FactoryBot.define do
3336
trait :other_region do

0 commit comments

Comments
 (0)