factory_bot 4.8.2 → 5.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.yardopts +1 -0
- data/GETTING_STARTED.md +226 -129
- data/LICENSE +1 -1
- data/NAME.md +12 -5
- data/NEWS.md +351 -0
- data/README.md +35 -27
- data/lib/factory_bot/aliases.rb +1 -1
- data/lib/factory_bot/attribute/dynamic.rb +1 -0
- data/lib/factory_bot/attribute.rb +4 -39
- data/lib/factory_bot/attribute_assigner.rb +21 -6
- data/lib/factory_bot/attribute_list.rb +2 -1
- data/lib/factory_bot/callback.rb +3 -2
- data/lib/factory_bot/configuration.rb +16 -20
- data/lib/factory_bot/declaration/association.rb +14 -1
- data/lib/factory_bot/declaration/dynamic.rb +3 -1
- data/lib/factory_bot/declaration/implicit.rb +7 -2
- data/lib/factory_bot/declaration.rb +4 -4
- data/lib/factory_bot/declaration_list.rb +1 -1
- data/lib/factory_bot/decorator/attribute_hash.rb +1 -1
- data/lib/factory_bot/decorator/invocation_tracker.rb +1 -1
- data/lib/factory_bot/decorator.rb +5 -1
- data/lib/factory_bot/definition.rb +10 -7
- data/lib/factory_bot/definition_hierarchy.rb +1 -11
- data/lib/factory_bot/definition_proxy.rb +65 -41
- data/lib/factory_bot/errors.rb +7 -4
- data/lib/factory_bot/evaluation.rb +1 -1
- data/lib/factory_bot/evaluator.rb +5 -5
- data/lib/factory_bot/factory.rb +8 -8
- data/lib/factory_bot/factory_runner.rb +3 -3
- data/lib/factory_bot/find_definitions.rb +1 -1
- data/lib/factory_bot/internal.rb +104 -0
- data/lib/factory_bot/linter.rb +36 -19
- data/lib/factory_bot/null_factory.rb +4 -1
- data/lib/factory_bot/null_object.rb +2 -2
- data/lib/factory_bot/registry.rb +15 -6
- data/lib/factory_bot/reload.rb +3 -3
- data/lib/factory_bot/sequence.rb +9 -1
- data/lib/factory_bot/strategy/null.rb +2 -4
- data/lib/factory_bot/strategy/stub.rb +32 -36
- data/lib/factory_bot/strategy_calculator.rb +1 -1
- data/lib/factory_bot/strategy_syntax_method_registrar.rb +13 -2
- data/lib/factory_bot/syntax/default.rb +12 -24
- data/lib/factory_bot/syntax/methods.rb +32 -9
- data/lib/factory_bot/syntax.rb +2 -2
- data/lib/factory_bot/trait.rb +6 -3
- data/lib/factory_bot/version.rb +1 -1
- data/lib/factory_bot.rb +103 -134
- metadata +79 -64
- data/.autotest +0 -9
- data/.gitignore +0 -10
- data/.rspec +0 -2
- data/.simplecov +0 -4
- data/.travis.yml +0 -38
- data/Appraisals +0 -19
- data/Gemfile +0 -8
- data/Gemfile.lock +0 -103
- data/NEWS +0 -293
- data/Rakefile +0 -36
- data/cucumber.yml +0 -1
- data/factory_bot.gemspec +0 -36
- data/factory_girl.gemspec +0 -40
- data/gemfiles/3.2.gemfile +0 -10
- data/gemfiles/3.2.gemfile.lock +0 -105
- data/gemfiles/4.0.gemfile +0 -10
- data/gemfiles/4.0.gemfile.lock +0 -105
- data/gemfiles/4.1.gemfile +0 -10
- data/gemfiles/4.1.gemfile.lock +0 -104
- data/gemfiles/4.2.gemfile +0 -10
- data/gemfiles/4.2.gemfile.lock +0 -104
- data/gemfiles/5.0.gemfile +0 -10
- data/gemfiles/5.0.gemfile.lock +0 -103
- data/lib/factory_bot/attribute/static.rb +0 -16
- data/lib/factory_bot/declaration/static.rb +0 -26
- data/lib/factory_bot/decorator/class_key_hash.rb +0 -28
- data/lib/factory_girl.rb +0 -5
@@ -1,37 +1,33 @@
|
|
1
1
|
module FactoryBot
|
2
2
|
# @api private
|
3
3
|
class Configuration
|
4
|
-
attr_reader
|
5
|
-
|
6
|
-
|
4
|
+
attr_reader(
|
5
|
+
:callback_names,
|
6
|
+
:factories,
|
7
|
+
:inline_sequences,
|
8
|
+
:sequences,
|
9
|
+
:strategies,
|
10
|
+
:traits,
|
11
|
+
)
|
7
12
|
|
8
13
|
def initialize
|
9
|
-
@factories = Decorator::DisallowsDuplicatesRegistry.new(Registry.new(
|
10
|
-
@sequences = Decorator::DisallowsDuplicatesRegistry.new(Registry.new(
|
11
|
-
@traits = Decorator::DisallowsDuplicatesRegistry.new(Registry.new(
|
12
|
-
@strategies = Registry.new(
|
14
|
+
@factories = Decorator::DisallowsDuplicatesRegistry.new(Registry.new("Factory"))
|
15
|
+
@sequences = Decorator::DisallowsDuplicatesRegistry.new(Registry.new("Sequence"))
|
16
|
+
@traits = Decorator::DisallowsDuplicatesRegistry.new(Registry.new("Trait"))
|
17
|
+
@strategies = Registry.new("Strategy")
|
13
18
|
@callback_names = Set.new
|
14
|
-
@definition = Definition.new
|
15
|
-
|
16
|
-
@allow_class_lookup = true
|
19
|
+
@definition = Definition.new(:configuration)
|
20
|
+
@inline_sequences = []
|
17
21
|
|
18
|
-
to_create
|
22
|
+
to_create(&:save!)
|
19
23
|
initialize_with { new }
|
20
24
|
end
|
21
25
|
|
22
26
|
delegate :to_create, :skip_create, :constructor, :before, :after,
|
23
|
-
|
27
|
+
:callback, :callbacks, to: :@definition
|
24
28
|
|
25
29
|
def initialize_with(&block)
|
26
30
|
@definition.define_constructor(&block)
|
27
31
|
end
|
28
|
-
|
29
|
-
def duplicate_attribute_assignment_from_initialize_with
|
30
|
-
false
|
31
|
-
end
|
32
|
-
|
33
|
-
def duplicate_attribute_assignment_from_initialize_with=(value)
|
34
|
-
ActiveSupport::Deprecation.warn 'Assignment of duplicate_attribute_assignment_from_initialize_with is unnecessary as this is now default behavior in FactoryBot 4.0; this line can be removed', caller
|
35
|
-
end
|
36
32
|
end
|
37
33
|
end
|
@@ -10,19 +10,32 @@ module FactoryBot
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def ==(other)
|
13
|
-
|
13
|
+
self.class == other.class &&
|
14
|
+
name == other.name &&
|
14
15
|
options == other.options
|
15
16
|
end
|
16
17
|
|
17
18
|
protected
|
19
|
+
|
18
20
|
attr_reader :options
|
19
21
|
|
20
22
|
private
|
21
23
|
|
22
24
|
def build
|
25
|
+
ensure_factory_is_not_a_declaration!
|
26
|
+
|
23
27
|
factory_name = @overrides[:factory] || name
|
24
28
|
[Attribute::Association.new(name, factory_name, [@traits, @overrides.except(:factory)].flatten)]
|
25
29
|
end
|
30
|
+
|
31
|
+
def ensure_factory_is_not_a_declaration!
|
32
|
+
if @overrides[:factory].is_a?(Declaration)
|
33
|
+
raise ArgumentError.new(<<~MSG)
|
34
|
+
Association '#{name}' received an invalid factory argument.
|
35
|
+
Did you mean? 'factory: :#{@overrides[:factory].name}'
|
36
|
+
MSG
|
37
|
+
end
|
38
|
+
end
|
26
39
|
end
|
27
40
|
end
|
28
41
|
end
|
@@ -8,12 +8,14 @@ module FactoryBot
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def ==(other)
|
11
|
-
|
11
|
+
self.class == other.class &&
|
12
|
+
name == other.name &&
|
12
13
|
factory == other.factory &&
|
13
14
|
ignored == other.ignored
|
14
15
|
end
|
15
16
|
|
16
17
|
protected
|
18
|
+
|
17
19
|
attr_reader :factory
|
18
20
|
|
19
21
|
private
|
@@ -21,8 +23,11 @@ module FactoryBot
|
|
21
23
|
def build
|
22
24
|
if FactoryBot.factories.registered?(name)
|
23
25
|
[Attribute::Association.new(name, name, {})]
|
24
|
-
elsif FactoryBot.sequences.registered?(name)
|
26
|
+
elsif FactoryBot::Internal.sequences.registered?(name)
|
25
27
|
[Attribute::Sequence.new(name, name, @ignored)]
|
28
|
+
elsif @factory.name.to_s == name.to_s
|
29
|
+
message = "Self-referencing trait '#{@name}'"
|
30
|
+
raise TraitDefinitionError, message
|
26
31
|
else
|
27
32
|
@factory.inherit_traits([name])
|
28
33
|
[]
|
@@ -1,7 +1,6 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require 'factory_bot/declaration/implicit'
|
1
|
+
require "factory_bot/declaration/dynamic"
|
2
|
+
require "factory_bot/declaration/association"
|
3
|
+
require "factory_bot/declaration/implicit"
|
5
4
|
|
6
5
|
module FactoryBot
|
7
6
|
# @api private
|
@@ -18,6 +17,7 @@ module FactoryBot
|
|
18
17
|
end
|
19
18
|
|
20
19
|
protected
|
20
|
+
|
21
21
|
attr_reader :ignored
|
22
22
|
end
|
23
23
|
end
|
@@ -6,10 +6,14 @@ module FactoryBot
|
|
6
6
|
@component = component
|
7
7
|
end
|
8
8
|
|
9
|
-
def method_missing(name, *args, &block)
|
9
|
+
def method_missing(name, *args, &block) # rubocop:disable Style/MethodMissingSuper
|
10
10
|
@component.send(name, *args, &block)
|
11
11
|
end
|
12
12
|
|
13
|
+
def respond_to_missing?(name, include_private = false)
|
14
|
+
@component.respond_to?(name, true) || super
|
15
|
+
end
|
16
|
+
|
13
17
|
def send(symbol, *args, &block)
|
14
18
|
__send__(symbol, *args, &block)
|
15
19
|
end
|
@@ -1,9 +1,10 @@
|
|
1
1
|
module FactoryBot
|
2
2
|
# @api private
|
3
3
|
class Definition
|
4
|
-
attr_reader :defined_traits, :declarations
|
4
|
+
attr_reader :defined_traits, :declarations, :name
|
5
5
|
|
6
|
-
def initialize(name
|
6
|
+
def initialize(name, base_traits = [])
|
7
|
+
@name = name
|
7
8
|
@declarations = DeclarationList.new(name)
|
8
9
|
@callbacks = []
|
9
10
|
@defined_traits = Set.new
|
@@ -48,7 +49,7 @@ module FactoryBot
|
|
48
49
|
|
49
50
|
defined_traits.each do |defined_trait|
|
50
51
|
base_traits.each { |bt| bt.define_trait defined_trait }
|
51
|
-
additional_traits.each { |
|
52
|
+
additional_traits.each { |at| at.define_trait defined_trait }
|
52
53
|
end
|
53
54
|
|
54
55
|
@compiled = true
|
@@ -73,7 +74,7 @@ module FactoryBot
|
|
73
74
|
end
|
74
75
|
|
75
76
|
def skip_create
|
76
|
-
@to_create = ->(instance) {
|
77
|
+
@to_create = ->(instance) {}
|
77
78
|
end
|
78
79
|
|
79
80
|
def define_trait(trait)
|
@@ -94,7 +95,7 @@ module FactoryBot
|
|
94
95
|
|
95
96
|
def callback(*names, &block)
|
96
97
|
names.each do |name|
|
97
|
-
FactoryBot.register_callback(name)
|
98
|
+
FactoryBot::Internal.register_callback(name)
|
98
99
|
add_callback(Callback.new(name, block))
|
99
100
|
end
|
100
101
|
end
|
@@ -110,17 +111,19 @@ module FactoryBot
|
|
110
111
|
end
|
111
112
|
|
112
113
|
def trait_by_name(name)
|
113
|
-
trait_for(name) ||
|
114
|
+
trait_for(name) || Internal.trait_by_name(name)
|
114
115
|
end
|
115
116
|
|
116
117
|
def trait_for(name)
|
117
|
-
defined_traits.
|
118
|
+
@defined_traits_by_name ||= defined_traits.each_with_object({}) { |t, memo| memo[t.name] ||= t }
|
119
|
+
@defined_traits_by_name[name.to_s]
|
118
120
|
end
|
119
121
|
|
120
122
|
def initialize_copy(source)
|
121
123
|
super
|
122
124
|
@attributes = nil
|
123
125
|
@compiled = false
|
126
|
+
@defined_traits_by_name = nil
|
124
127
|
end
|
125
128
|
|
126
129
|
def aggregate_from_traits_and_self(method_name, &block)
|
@@ -1,16 +1,6 @@
|
|
1
1
|
module FactoryBot
|
2
2
|
class DefinitionHierarchy
|
3
|
-
|
4
|
-
FactoryBot.callbacks
|
5
|
-
end
|
6
|
-
|
7
|
-
def constructor
|
8
|
-
FactoryBot.constructor
|
9
|
-
end
|
10
|
-
|
11
|
-
def to_create
|
12
|
-
FactoryBot.to_create
|
13
|
-
end
|
3
|
+
delegate :callbacks, :constructor, :to_create, to: Internal
|
14
4
|
|
15
5
|
def self.build_from_definition(definition)
|
16
6
|
build_to_create(&definition.to_create)
|
@@ -1,6 +1,19 @@
|
|
1
1
|
module FactoryBot
|
2
2
|
class DefinitionProxy
|
3
|
-
UNPROXIED_METHODS = %w(
|
3
|
+
UNPROXIED_METHODS = %w(
|
4
|
+
__send__
|
5
|
+
__id__
|
6
|
+
nil?
|
7
|
+
send
|
8
|
+
object_id
|
9
|
+
extend
|
10
|
+
instance_eval
|
11
|
+
initialize
|
12
|
+
block_given?
|
13
|
+
raise
|
14
|
+
caller
|
15
|
+
method
|
16
|
+
).freeze
|
4
17
|
|
5
18
|
(instance_methods + private_instance_methods).each do |method|
|
6
19
|
undef_method(method) unless UNPROXIED_METHODS.include?(method.to_s)
|
@@ -21,42 +34,21 @@ module FactoryBot
|
|
21
34
|
raise FactoryBot::MethodDefinitionError, message
|
22
35
|
end
|
23
36
|
|
24
|
-
# Adds an attribute
|
25
|
-
#
|
26
|
-
#
|
27
|
-
#
|
28
|
-
# called with a block, the attribute will be generated "lazily," whenever an
|
29
|
-
# instance is generated. Lazy attribute blocks will not be called if that
|
37
|
+
# Adds an attribute to the factory.
|
38
|
+
# The attribute value will be generated "lazily"
|
39
|
+
# by calling the block whenever an instance is generated.
|
40
|
+
# The block will not be called if the
|
30
41
|
# attribute is overridden for a specific instance.
|
31
42
|
#
|
32
|
-
# When defining lazy attributes, an instance of FactoryBot::Strategy will
|
33
|
-
# be yielded, allowing associations to be built using the correct build
|
34
|
-
# strategy.
|
35
|
-
#
|
36
43
|
# Arguments:
|
37
44
|
# * name: +Symbol+ or +String+
|
38
45
|
# The name of this attribute. This will be assigned using "name=" for
|
39
46
|
# generated instances.
|
40
|
-
|
41
|
-
|
42
|
-
def add_attribute(name, value = nil, &block)
|
43
|
-
raise AttributeDefinitionError, 'Both value and block given' if value && block_given?
|
44
|
-
|
45
|
-
declaration = if block_given?
|
46
|
-
Declaration::Dynamic.new(name, @ignore, block)
|
47
|
-
else
|
48
|
-
Declaration::Static.new(name, value, @ignore)
|
49
|
-
end
|
50
|
-
|
47
|
+
def add_attribute(name, &block)
|
48
|
+
declaration = Declaration::Dynamic.new(name, @ignore, block)
|
51
49
|
@definition.declare_attribute(declaration)
|
52
50
|
end
|
53
51
|
|
54
|
-
def ignore(&block)
|
55
|
-
ActiveSupport::Deprecation.warn "`#ignore` is deprecated and will be "\
|
56
|
-
"removed in 5.0. Please use `#transient` instead."
|
57
|
-
transient(&block)
|
58
|
-
end
|
59
|
-
|
60
52
|
def transient(&block)
|
61
53
|
proxy = DefinitionProxy.new(@definition, true)
|
62
54
|
proxy.instance_eval(&block)
|
@@ -66,40 +58,48 @@ module FactoryBot
|
|
66
58
|
# attribute, so that:
|
67
59
|
#
|
68
60
|
# factory :user do
|
69
|
-
# name 'Billy Idol'
|
61
|
+
# name { 'Billy Idol' }
|
70
62
|
# end
|
71
63
|
#
|
72
64
|
# and:
|
73
65
|
#
|
74
66
|
# factory :user do
|
75
|
-
# add_attribute
|
67
|
+
# add_attribute(:name) { 'Billy Idol' }
|
76
68
|
# end
|
77
69
|
#
|
78
70
|
# are equivalent.
|
79
71
|
#
|
80
|
-
# If no argument or block is given, factory_bot will look for
|
81
|
-
#
|
72
|
+
# If no argument or block is given, factory_bot will first look for an
|
73
|
+
# association, then for a sequence, and finally for a trait with the same
|
74
|
+
# name. This means that given an "admin" trait, an "email" sequence, and an
|
75
|
+
# "account" factory:
|
82
76
|
#
|
83
|
-
# factory :user do
|
84
|
-
# email {
|
77
|
+
# factory :user, traits: [:admin] do
|
78
|
+
# email { generate(:email) }
|
85
79
|
# association :account
|
86
80
|
# end
|
87
81
|
#
|
88
82
|
# and:
|
89
83
|
#
|
90
84
|
# factory :user do
|
85
|
+
# admin
|
91
86
|
# email
|
92
87
|
# account
|
93
88
|
# end
|
94
89
|
#
|
95
90
|
# are equivalent.
|
96
|
-
def method_missing(name, *args, &block)
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
91
|
+
def method_missing(name, *args, &block) # rubocop:disable Style/MissingRespondToMissing, Style/MethodMissingSuper
|
92
|
+
association_options = args.first
|
93
|
+
|
94
|
+
if association_options.nil?
|
95
|
+
__declare_attribute__(name, block)
|
96
|
+
elsif __valid_association_options?(association_options)
|
97
|
+
association(name, association_options)
|
101
98
|
else
|
102
|
-
|
99
|
+
raise NoMethodError.new(<<~MSG)
|
100
|
+
undefined method '#{name}' in '#{@definition.name}' factory
|
101
|
+
Did you mean? '#{name} { #{association_options.inspect} }'
|
102
|
+
MSG
|
103
103
|
end
|
104
104
|
end
|
105
105
|
|
@@ -121,6 +121,7 @@ module FactoryBot
|
|
121
121
|
# Except that no globally available sequence will be defined.
|
122
122
|
def sequence(name, *args, &block)
|
123
123
|
sequence = Sequence.new(name, *args, &block)
|
124
|
+
FactoryBot::Internal.register_inline_sequence(sequence)
|
124
125
|
add_attribute(name) { increment_sequence(sequence) }
|
125
126
|
end
|
126
127
|
|
@@ -148,7 +149,15 @@ module FactoryBot
|
|
148
149
|
# name of the factory. For example, a "user" association will by
|
149
150
|
# default use the "user" factory.
|
150
151
|
def association(name, *options)
|
151
|
-
|
152
|
+
if block_given?
|
153
|
+
raise AssociationDefinitionError.new(
|
154
|
+
"Unexpected block passed to '#{name}' association "\
|
155
|
+
"in '#{@definition.name}' factory",
|
156
|
+
)
|
157
|
+
else
|
158
|
+
declaration = Declaration::Association.new(name, *options)
|
159
|
+
@definition.declare_attribute(declaration)
|
160
|
+
end
|
152
161
|
end
|
153
162
|
|
154
163
|
def to_create(&block)
|
@@ -170,5 +179,20 @@ module FactoryBot
|
|
170
179
|
def initialize_with(&block)
|
171
180
|
@definition.define_constructor(&block)
|
172
181
|
end
|
182
|
+
|
183
|
+
private
|
184
|
+
|
185
|
+
def __declare_attribute__(name, block)
|
186
|
+
if block.nil?
|
187
|
+
declaration = Declaration::Implicit.new(name, @definition, @ignore)
|
188
|
+
@definition.declare_attribute(declaration)
|
189
|
+
else
|
190
|
+
add_attribute(name, &block)
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
def __valid_association_options?(options)
|
195
|
+
options.respond_to?(:has_key?) && options.has_key?(:factory)
|
196
|
+
end
|
173
197
|
end
|
174
198
|
end
|
data/lib/factory_bot/errors.rb
CHANGED
@@ -2,6 +2,9 @@ module FactoryBot
|
|
2
2
|
# Raised when a factory is defined that attempts to instantiate itself.
|
3
3
|
class AssociationDefinitionError < RuntimeError; end
|
4
4
|
|
5
|
+
# Raised when a trait is defined that references itself.
|
6
|
+
class TraitDefinitionError < RuntimeError; end
|
7
|
+
|
5
8
|
# Raised when a callback is defined that has an invalid name
|
6
9
|
class InvalidCallbackNameError < RuntimeError; end
|
7
10
|
|
@@ -11,12 +14,12 @@ module FactoryBot
|
|
11
14
|
# Raised when attempting to register a sequence from a dynamic attribute block
|
12
15
|
class SequenceAbuseError < RuntimeError; end
|
13
16
|
|
14
|
-
# Raised when defining an
|
15
|
-
# * Defining an attribute which has a name ending in "="
|
16
|
-
# * Defining an attribute with both a static and lazy value
|
17
|
-
# * Defining an attribute twice in the same factory
|
17
|
+
# Raised when defining an attribute twice in the same factory
|
18
18
|
class AttributeDefinitionError < RuntimeError; end
|
19
19
|
|
20
|
+
# Raised when attempting to pass a block to an association definition
|
21
|
+
class AssociationDefinitionError < RuntimeError; end
|
22
|
+
|
20
23
|
# Raised when a method is defined in a factory or trait with arguments
|
21
24
|
class MethodDefinitionError < RuntimeError; end
|
22
25
|
|
@@ -1,5 +1,5 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "active_support/core_ext/hash/except"
|
2
|
+
require "active_support/core_ext/class/attribute"
|
3
3
|
|
4
4
|
module FactoryBot
|
5
5
|
# @api private
|
@@ -37,7 +37,7 @@ module FactoryBot
|
|
37
37
|
@instance = object_instance
|
38
38
|
end
|
39
39
|
|
40
|
-
def method_missing(method_name, *args, &block)
|
40
|
+
def method_missing(method_name, *args, &block) # rubocop:disable Style/MethodMissingSuper
|
41
41
|
if @instance.respond_to?(method_name)
|
42
42
|
@instance.send(method_name, *args, &block)
|
43
43
|
else
|
@@ -45,7 +45,7 @@ module FactoryBot
|
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
|
-
def respond_to_missing?(method_name,
|
48
|
+
def respond_to_missing?(method_name, _include_private = false)
|
49
49
|
@instance.respond_to?(method_name) || SyntaxRunner.new.respond_to?(method_name)
|
50
50
|
end
|
51
51
|
|
@@ -66,7 +66,7 @@ module FactoryBot
|
|
66
66
|
end
|
67
67
|
|
68
68
|
def self.define_attribute(name, &block)
|
69
|
-
if
|
69
|
+
if instance_methods(false).include?(name) || private_instance_methods(false).include?(name)
|
70
70
|
undef_method(name)
|
71
71
|
end
|
72
72
|
|
data/lib/factory_bot/factory.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "active_support/core_ext/hash/keys"
|
2
|
+
require "active_support/inflector"
|
3
3
|
|
4
4
|
module FactoryBot
|
5
5
|
# @api private
|
@@ -21,10 +21,10 @@ module FactoryBot
|
|
21
21
|
|
22
22
|
def build_class
|
23
23
|
@build_class ||= if class_name.is_a? Class
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
24
|
+
class_name
|
25
|
+
else
|
26
|
+
class_name.to_s.camelize.constantize
|
27
|
+
end
|
28
28
|
end
|
29
29
|
|
30
30
|
def run(build_strategy, overrides, &block)
|
@@ -91,7 +91,7 @@ module FactoryBot
|
|
91
91
|
end
|
92
92
|
|
93
93
|
def with_traits(traits)
|
94
|
-
|
94
|
+
clone.tap do |factory_with_traits|
|
95
95
|
factory_with_traits.append_traits traits
|
96
96
|
end
|
97
97
|
end
|
@@ -145,7 +145,7 @@ module FactoryBot
|
|
145
145
|
|
146
146
|
def parent
|
147
147
|
if @parent
|
148
|
-
FactoryBot.factory_by_name(@parent)
|
148
|
+
FactoryBot::Internal.factory_by_name(@parent)
|
149
149
|
else
|
150
150
|
NullFactory.new
|
151
151
|
end
|
@@ -9,7 +9,7 @@ module FactoryBot
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def run(runner_strategy = @strategy, &block)
|
12
|
-
factory = FactoryBot.factory_by_name(@name)
|
12
|
+
factory = FactoryBot::Internal.factory_by_name(@name)
|
13
13
|
|
14
14
|
factory.compile
|
15
15
|
|
@@ -22,10 +22,10 @@ module FactoryBot
|
|
22
22
|
strategy: runner_strategy,
|
23
23
|
traits: @traits,
|
24
24
|
overrides: @overrides,
|
25
|
-
factory: factory
|
25
|
+
factory: factory,
|
26
26
|
}
|
27
27
|
|
28
|
-
ActiveSupport::Notifications.instrument(
|
28
|
+
ActiveSupport::Notifications.instrument("factory_bot.run_factory", instrumentation_payload) do
|
29
29
|
factory.run(runner_strategy, @overrides, &block)
|
30
30
|
end
|
31
31
|
end
|