Skip to content

Commit cd61157

Browse files
authored
Merge pull request #23613 from jrafanie/support-factory-bot-rails
Support factory bot rails
2 parents 7a92ea8 + 028e3d0 commit cd61157

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

spec/factories/miq_region.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
FactoryBot.define do
2-
region_remote = MiqRegion.my_region_number
3-
42
factory :miq_region do
5-
sequence(:region) { |region| region == region_remote ? region + 1 : region }
3+
sequence(:region) do |region|
4+
region == ApplicationRecord.region_number_from_sequence ? region + 1 : region
5+
end
66
end
77
end

spec/support/factory_bot_helper.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,23 @@ def seq_padded_for_sorting(n)
1919

2020
require 'factory_bot'
2121

22-
# In case we are running as an engine, the factories are located in the dummy app
23-
FactoryBot.definition_file_paths << 'spec/manageiq/spec/factories'
22+
# In case we are running as an engine, the factories are located in the Rails application
23+
FactoryBot.definition_file_paths << Rails.root.join('spec/factories')
2424

2525
# Also, add factories from provider gems until miq codebase does not use any provider specific factories anymore
2626
Rails::Engine.subclasses.select { |e| e.name.starts_with?("ManageIQ::Providers") }.each do |engine|
27-
FactoryBot.definition_file_paths << File.join(engine.root, 'spec', 'factories')
27+
FactoryBot.definition_file_paths << engine.root.join('spec', 'factories')
2828
end
2929

30-
FactoryBot.find_definitions
30+
# Prefer setting the definition_file_paths and call FactoryBot.reload, which will recreate the configuration
31+
# and call find_definitions.
32+
#
33+
# find_definitions is not reentrant, so a second call will lead to loading already registered factories.
34+
# Other code, such as factory bot rails, may have already registered factories, so we can't assume
35+
# we're the first to call find_definitions.
36+
FactoryBot.definition_file_paths = FactoryBot.definition_file_paths.uniq
37+
38+
FactoryBot.reload
3139

3240
FactoryBot.define do
3341
trait :other_region do

0 commit comments

Comments
 (0)