File tree Expand file tree Collapse file tree 4 files changed +62
-1
lines changed Expand file tree Collapse file tree 4 files changed +62
-1
lines changed Original file line number Diff line number Diff line change @@ -77,6 +77,17 @@ class MyEngine < ::Rails::Engine
77
77
end
78
78
```
79
79
80
+ If your application has a large number of factories, you may want to enable
81
+ lazy loading to speed up boot time:
82
+
83
+ ``` rb
84
+ config.factory_bot.lazy_load_definitions = true
85
+ ```
86
+
87
+ With lazy loading of definitions enabled, ` FactoryBot.factories ` will appear
88
+ empty after booting the application. All factory definitions will be loaded
89
+ when any factory is first accessed by name.
90
+
80
91
You can also disable automatic factory definition loading entirely by
81
92
using an empty array:
82
93
Original file line number Diff line number Diff line change @@ -197,3 +197,36 @@ Feature: automatically load factory definitions
197
197
"""
198
198
When I run `bundle exec rake test` with a clean environment
199
199
Then the output should contain "2 assertions, 0 failures, 0 errors"
200
+
201
+ Scenario : use lazy loading of factory definitions
202
+ When I configure the factories as:
203
+ """
204
+ config.factory_bot.lazy_load_definitions = true
205
+ """
206
+ When I write to "test/factories.rb" with:
207
+ """
208
+ FactoryBot.define do
209
+ factory :user do
210
+ name { "Frank" }
211
+ end
212
+ end
213
+ """
214
+ When I write to "test/unit/user_test.rb" with:
215
+ """
216
+ require 'test_helper'
217
+
218
+ class UserTest < ActiveSupport::TestCase
219
+ test "use lazy loaded factory" do
220
+ assert FactoryBot.factories.none?
221
+ refute FactoryBot.factories.registered?(:user)
222
+
223
+ user = FactoryBot.create(:user)
224
+ assert_equal 'Frank', user.name
225
+
226
+ assert FactoryBot.factories.any?
227
+ assert FactoryBot.factories.registered?(:user)
228
+ end
229
+ end
230
+ """
231
+ When I run `bundle exec rake test` with a clean environment
232
+ Then the output should contain "5 assertions, 0 failures, 0 errors"
Original file line number Diff line number Diff line change
1
+ require "factory_bot/registry"
2
+
3
+ module FactoryBotRails
4
+ module LazyRegistryFind
5
+ def find ( *)
6
+ FactoryBot . find_definitions unless FactoryBot . factories . any?
7
+ super
8
+ end
9
+ end
10
+ end
Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ module FactoryBotRails
10
10
class Railtie < Rails ::Railtie
11
11
config . factory_bot = ActiveSupport ::OrderedOptions . new
12
12
config . factory_bot . definition_file_paths = FactoryBot . definition_file_paths
13
+ config . factory_bot . lazy_load_definitions = false
13
14
config . factory_bot . validator = FactoryBotRails ::FactoryValidator . new
14
15
15
16
initializer "factory_bot.set_fixture_replacement" do
@@ -21,7 +22,13 @@ class Railtie < Rails::Railtie
21
22
end
22
23
23
24
config . after_initialize do |app |
24
- FactoryBot . find_definitions
25
+ if app . config . factory_bot . lazy_load_definitions && !app . config . eager_load
26
+ require "factory_bot_rails/lazy_registry_find"
27
+ FactoryBot ::Registry . prepend FactoryBotRails ::LazyRegistryFind
28
+ else
29
+ FactoryBot . find_definitions
30
+ end
31
+
25
32
Reloader . new ( app ) . run
26
33
app . config . factory_bot . validator . run
27
34
end
You can’t perform that action at this time.
0 commit comments