factory_bot 5.2.0 → 6.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CONTRIBUTING.md +52 -13
- data/GETTING_STARTED.md +415 -65
- data/NEWS.md +9 -2
- data/lib/factory_bot.rb +19 -53
- data/lib/factory_bot/aliases.rb +3 -3
- data/lib/factory_bot/attribute/association.rb +2 -2
- data/lib/factory_bot/attribute/dynamic.rb +1 -1
- data/lib/factory_bot/attribute_assigner.rb +9 -10
- data/lib/factory_bot/attribute_list.rb +1 -1
- data/lib/factory_bot/callback.rb +2 -10
- data/lib/factory_bot/configuration.rb +7 -7
- data/lib/factory_bot/declaration.rb +1 -1
- data/lib/factory_bot/declaration_list.rb +2 -2
- data/lib/factory_bot/decorator.rb +0 -4
- data/lib/factory_bot/definition.rb +41 -15
- data/lib/factory_bot/definition_proxy.rb +63 -5
- data/lib/factory_bot/enum.rb +27 -0
- data/lib/factory_bot/evaluator.rb +4 -6
- data/lib/factory_bot/evaluator_class_definer.rb +1 -1
- data/lib/factory_bot/factory.rb +12 -12
- data/lib/factory_bot/factory_runner.rb +3 -3
- data/lib/factory_bot/find_definitions.rb +1 -1
- data/lib/factory_bot/internal.rb +12 -25
- data/lib/factory_bot/linter.rb +8 -12
- data/lib/factory_bot/null_factory.rb +11 -5
- data/lib/factory_bot/null_object.rb +2 -6
- data/lib/factory_bot/registry.rb +2 -2
- data/lib/factory_bot/reload.rb +0 -1
- data/lib/factory_bot/sequence.rb +5 -5
- data/lib/factory_bot/strategy/null.rb +4 -2
- data/lib/factory_bot/strategy/stub.rb +6 -2
- data/lib/factory_bot/syntax/default.rb +6 -6
- data/lib/factory_bot/trait.rb +1 -1
- data/lib/factory_bot/version.rb +1 -1
- metadata +9 -37
@@ -0,0 +1,27 @@
|
|
1
|
+
module FactoryBot
|
2
|
+
# @api private
|
3
|
+
class Enum
|
4
|
+
def initialize(attribute_name, values = nil)
|
5
|
+
@attribute_name = attribute_name
|
6
|
+
@values = values
|
7
|
+
end
|
8
|
+
|
9
|
+
def build_traits(klass)
|
10
|
+
enum_values(klass).map do |trait_name, value|
|
11
|
+
build_trait(trait_name, @attribute_name, value || trait_name)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def enum_values(klass)
|
18
|
+
@values || klass.send(@attribute_name.to_s.pluralize)
|
19
|
+
end
|
20
|
+
|
21
|
+
def build_trait(trait_name, attribute_name, value)
|
22
|
+
Trait.new(trait_name) do
|
23
|
+
add_attribute(attribute_name) { value }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -7,7 +7,7 @@ module FactoryBot
|
|
7
7
|
class_attribute :attribute_lists
|
8
8
|
|
9
9
|
private_instance_methods.each do |method|
|
10
|
-
undef_method(method) unless method
|
10
|
+
undef_method(method) unless method.match?(/^__|initialize/)
|
11
11
|
end
|
12
12
|
|
13
13
|
def initialize(build_strategy, overrides = {})
|
@@ -23,9 +23,9 @@ module FactoryBot
|
|
23
23
|
|
24
24
|
def association(factory_name, *traits_and_overrides)
|
25
25
|
overrides = traits_and_overrides.extract_options!
|
26
|
-
strategy_override = overrides.fetch(:strategy)
|
26
|
+
strategy_override = overrides.fetch(:strategy) {
|
27
27
|
FactoryBot.use_parent_strategy ? @build_strategy.class : :create
|
28
|
-
|
28
|
+
}
|
29
29
|
|
30
30
|
traits_and_overrides += [overrides.except(:strategy)]
|
31
31
|
|
@@ -33,9 +33,7 @@ module FactoryBot
|
|
33
33
|
@build_strategy.association(runner)
|
34
34
|
end
|
35
35
|
|
36
|
-
|
37
|
-
@instance = object_instance
|
38
|
-
end
|
36
|
+
attr_writer :instance
|
39
37
|
|
40
38
|
def method_missing(method_name, *args, &block) # rubocop:disable Style/MethodMissingSuper
|
41
39
|
if @instance.respond_to?(method_name)
|
@@ -3,7 +3,7 @@ module FactoryBot
|
|
3
3
|
class EvaluatorClassDefiner
|
4
4
|
def initialize(attributes, parent_class)
|
5
5
|
@parent_class = parent_class
|
6
|
-
@attributes
|
6
|
+
@attributes = attributes
|
7
7
|
|
8
8
|
attributes.each do |attribute|
|
9
9
|
evaluator_class.define_attribute(attribute.name, &attribute.to_proc)
|
data/lib/factory_bot/factory.rb
CHANGED
@@ -8,23 +8,23 @@ module FactoryBot
|
|
8
8
|
|
9
9
|
def initialize(name, options = {})
|
10
10
|
assert_valid_options(options)
|
11
|
-
@name
|
12
|
-
@parent
|
13
|
-
@aliases
|
14
|
-
@class_name
|
15
|
-
@definition
|
16
|
-
@compiled
|
11
|
+
@name = name.respond_to?(:to_sym) ? name.to_sym : name.to_s.underscore.to_sym
|
12
|
+
@parent = options[:parent]
|
13
|
+
@aliases = options[:aliases] || []
|
14
|
+
@class_name = options[:class]
|
15
|
+
@definition = Definition.new(@name, options[:traits] || [])
|
16
|
+
@compiled = false
|
17
17
|
end
|
18
18
|
|
19
19
|
delegate :add_callback, :declare_attribute, :to_create, :define_trait, :constructor,
|
20
|
-
|
20
|
+
:defined_traits, :inherit_traits, :append_traits, to: :@definition
|
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)
|
@@ -84,7 +84,7 @@ module FactoryBot
|
|
84
84
|
unless @compiled
|
85
85
|
parent.compile
|
86
86
|
parent.defined_traits.each { |trait| define_trait(trait) }
|
87
|
-
@definition.compile
|
87
|
+
@definition.compile(build_class)
|
88
88
|
build_hierarchy
|
89
89
|
@compiled = true
|
90
90
|
end
|
@@ -1,11 +1,11 @@
|
|
1
1
|
module FactoryBot
|
2
2
|
class FactoryRunner
|
3
3
|
def initialize(name, strategy, traits_and_overrides)
|
4
|
-
@name
|
4
|
+
@name = name
|
5
5
|
@strategy = strategy
|
6
6
|
|
7
7
|
@overrides = traits_and_overrides.extract_options!
|
8
|
-
@traits
|
8
|
+
@traits = traits_and_overrides
|
9
9
|
end
|
10
10
|
|
11
11
|
def run(runner_strategy = @strategy, &block)
|
@@ -22,7 +22,7 @@ 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
28
|
ActiveSupport::Notifications.instrument("factory_bot.run_factory", instrumentation_payload) do
|
@@ -7,7 +7,7 @@ module FactoryBot
|
|
7
7
|
attr_accessor :definition_file_paths
|
8
8
|
end
|
9
9
|
|
10
|
-
self.definition_file_paths = %w
|
10
|
+
self.definition_file_paths = %w[factories test/factories spec/factories]
|
11
11
|
|
12
12
|
def self.find_definitions
|
13
13
|
absolute_definition_file_paths = definition_file_paths.map { |path| File.expand_path(path) }
|
data/lib/factory_bot/internal.rb
CHANGED
@@ -3,19 +3,18 @@ module FactoryBot
|
|
3
3
|
module Internal
|
4
4
|
class << self
|
5
5
|
delegate :after,
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
to: :configuration
|
6
|
+
:before,
|
7
|
+
:callbacks,
|
8
|
+
:constructor,
|
9
|
+
:factories,
|
10
|
+
:initialize_with,
|
11
|
+
:inline_sequences,
|
12
|
+
:sequences,
|
13
|
+
:skip_create,
|
14
|
+
:strategies,
|
15
|
+
:to_create,
|
16
|
+
:traits,
|
17
|
+
to: :configuration
|
19
18
|
|
20
19
|
def configuration
|
21
20
|
@configuration ||= Configuration.new
|
@@ -87,18 +86,6 @@ module FactoryBot
|
|
87
86
|
register_strategy(:build_stubbed, FactoryBot::Strategy::Stub)
|
88
87
|
register_strategy(:null, FactoryBot::Strategy::Null)
|
89
88
|
end
|
90
|
-
|
91
|
-
def register_default_callbacks
|
92
|
-
register_callback(:after_create)
|
93
|
-
register_callback(:after_build)
|
94
|
-
register_callback(:after_stub)
|
95
|
-
register_callback(:before_create)
|
96
|
-
end
|
97
|
-
|
98
|
-
def register_callback(name)
|
99
|
-
name = name.to_sym
|
100
|
-
callback_names << name
|
101
|
-
end
|
102
89
|
end
|
103
90
|
end
|
104
91
|
end
|
data/lib/factory_bot/linter.rb
CHANGED
@@ -19,17 +19,16 @@ module FactoryBot
|
|
19
19
|
attr_reader :factories_to_lint, :invalid_factories, :factory_strategy
|
20
20
|
|
21
21
|
def calculate_invalid_factories
|
22
|
-
factories_to_lint.
|
22
|
+
factories_to_lint.each_with_object(Hash.new([])) do |factory, result|
|
23
23
|
errors = lint(factory)
|
24
24
|
result[factory] |= errors unless errors.empty?
|
25
|
-
result
|
26
25
|
end
|
27
26
|
end
|
28
27
|
|
29
28
|
class FactoryError
|
30
29
|
def initialize(wrapped_error, factory)
|
31
30
|
@wrapped_error = wrapped_error
|
32
|
-
@factory
|
31
|
+
@factory = factory
|
33
32
|
end
|
34
33
|
|
35
34
|
def message
|
@@ -72,7 +71,7 @@ module FactoryBot
|
|
72
71
|
result = []
|
73
72
|
begin
|
74
73
|
FactoryBot.public_send(factory_strategy, factory.name)
|
75
|
-
rescue
|
74
|
+
rescue => e
|
76
75
|
result |= [FactoryError.new(e, factory)]
|
77
76
|
end
|
78
77
|
result
|
@@ -81,20 +80,17 @@ module FactoryBot
|
|
81
80
|
def lint_traits(factory)
|
82
81
|
result = []
|
83
82
|
factory.definition.defined_traits.map(&:name).each do |trait_name|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
result |=
|
88
|
-
[FactoryTraitError.new(e, factory, trait_name)]
|
89
|
-
end
|
83
|
+
FactoryBot.public_send(factory_strategy, factory.name, trait_name)
|
84
|
+
rescue => e
|
85
|
+
result |= [FactoryTraitError.new(e, factory, trait_name)]
|
90
86
|
end
|
91
87
|
result
|
92
88
|
end
|
93
89
|
|
94
90
|
def error_message
|
95
|
-
lines = invalid_factories.map
|
91
|
+
lines = invalid_factories.map { |_factory, exceptions|
|
96
92
|
exceptions.map(&error_message_type)
|
97
|
-
|
93
|
+
}.flatten
|
98
94
|
|
99
95
|
<<~ERROR_MESSAGE.strip
|
100
96
|
The following factories are invalid:
|
@@ -8,14 +8,20 @@ module FactoryBot
|
|
8
8
|
end
|
9
9
|
|
10
10
|
delegate :defined_traits, :callbacks, :attributes, :constructor,
|
11
|
-
|
11
|
+
:to_create, to: :definition
|
12
12
|
|
13
|
-
def compile
|
13
|
+
def compile
|
14
|
+
end
|
14
15
|
|
15
|
-
def class_name
|
16
|
+
def class_name
|
17
|
+
end
|
16
18
|
|
17
|
-
def evaluator_class
|
19
|
+
def evaluator_class
|
20
|
+
FactoryBot::Evaluator
|
21
|
+
end
|
18
22
|
|
19
|
-
def hierarchy_class
|
23
|
+
def hierarchy_class
|
24
|
+
FactoryBot::DefinitionHierarchy
|
25
|
+
end
|
20
26
|
end
|
21
27
|
end
|
@@ -5,7 +5,7 @@ module FactoryBot
|
|
5
5
|
@methods_to_respond_to = methods_to_respond_to.map(&:to_s)
|
6
6
|
end
|
7
7
|
|
8
|
-
def method_missing(name, *args, &block)
|
8
|
+
def method_missing(name, *args, &block) # rubocop:disable Style/MissingRespondToMissing
|
9
9
|
if respond_to?(name)
|
10
10
|
nil
|
11
11
|
else
|
@@ -13,12 +13,8 @@ module FactoryBot
|
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
-
def respond_to?(method
|
16
|
+
def respond_to?(method)
|
17
17
|
@methods_to_respond_to.include? method.to_s
|
18
18
|
end
|
19
|
-
|
20
|
-
def respond_to_missing?(*)
|
21
|
-
false
|
22
|
-
end
|
23
19
|
end
|
24
20
|
end
|
data/lib/factory_bot/registry.rb
CHANGED
@@ -7,7 +7,7 @@ module FactoryBot
|
|
7
7
|
attr_reader :name
|
8
8
|
|
9
9
|
def initialize(name)
|
10
|
-
@name
|
10
|
+
@name = name
|
11
11
|
@items = ActiveSupport::HashWithIndifferentAccess.new
|
12
12
|
end
|
13
13
|
|
@@ -25,7 +25,7 @@ module FactoryBot
|
|
25
25
|
raise key_error_with_custom_message(e)
|
26
26
|
end
|
27
27
|
|
28
|
-
alias
|
28
|
+
alias [] find
|
29
29
|
|
30
30
|
def register(name, item)
|
31
31
|
@items[name] = item
|
data/lib/factory_bot/reload.rb
CHANGED
data/lib/factory_bot/sequence.rb
CHANGED
@@ -6,14 +6,14 @@ module FactoryBot
|
|
6
6
|
attr_reader :name
|
7
7
|
|
8
8
|
def initialize(name, *args, &proc)
|
9
|
-
@name
|
10
|
-
@proc
|
9
|
+
@name = name
|
10
|
+
@proc = proc
|
11
11
|
|
12
|
-
options
|
13
|
-
@value
|
12
|
+
options = args.extract_options!
|
13
|
+
@value = args.first || 1
|
14
14
|
@aliases = options.fetch(:aliases) { [] }
|
15
15
|
|
16
|
-
|
16
|
+
unless @value.respond_to?(:peek)
|
17
17
|
@value = EnumeratorAdapter.new(@value)
|
18
18
|
end
|
19
19
|
end
|
@@ -21,9 +21,13 @@ module FactoryBot
|
|
21
21
|
:update_attributes!,
|
22
22
|
:update_attributes,
|
23
23
|
:update_column,
|
24
|
-
:update_columns
|
24
|
+
:update_columns
|
25
25
|
].freeze
|
26
26
|
|
27
|
+
def self.next_id=(id)
|
28
|
+
@@next_id = id
|
29
|
+
end
|
30
|
+
|
27
31
|
def association(runner)
|
28
32
|
runner.run(:build_stubbed)
|
29
33
|
end
|
@@ -64,7 +68,7 @@ module FactoryBot
|
|
64
68
|
DISABLED_PERSISTENCE_METHODS.each do |write_method|
|
65
69
|
define_singleton_method(write_method) do |*args|
|
66
70
|
raise "stubbed models are not allowed to access the database - "\
|
67
|
-
"#{self.class}##{write_method}(#{args.join(
|
71
|
+
"#{self.class}##{write_method}(#{args.join(",")})"
|
68
72
|
end
|
69
73
|
end
|
70
74
|
end
|
@@ -38,12 +38,12 @@ module FactoryBot
|
|
38
38
|
end
|
39
39
|
|
40
40
|
delegate :after,
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
41
|
+
:before,
|
42
|
+
:callback,
|
43
|
+
:initialize_with,
|
44
|
+
:skip_create,
|
45
|
+
:to_create,
|
46
|
+
to: FactoryBot::Internal
|
47
47
|
end
|
48
48
|
|
49
49
|
class ModifyDSL
|
data/lib/factory_bot/trait.rb
CHANGED
data/lib/factory_bot/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: factory_bot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 6.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Josh Clayton
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2020-
|
12
|
+
date: 2020-06-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -17,14 +17,14 @@ dependencies:
|
|
17
17
|
requirements:
|
18
18
|
- - ">="
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version:
|
20
|
+
version: 5.0.0
|
21
21
|
type: :runtime
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
25
|
- - ">="
|
26
26
|
- !ruby/object:Gem::Version
|
27
|
-
version:
|
27
|
+
version: 5.0.0
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: activerecord
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
@@ -124,35 +124,7 @@ dependencies:
|
|
124
124
|
- !ruby/object:Gem::Version
|
125
125
|
version: '0'
|
126
126
|
- !ruby/object:Gem::Dependency
|
127
|
-
name:
|
128
|
-
requirement: !ruby/object:Gem::Requirement
|
129
|
-
requirements:
|
130
|
-
- - ">="
|
131
|
-
- !ruby/object:Gem::Version
|
132
|
-
version: '0'
|
133
|
-
type: :development
|
134
|
-
prerelease: false
|
135
|
-
version_requirements: !ruby/object:Gem::Requirement
|
136
|
-
requirements:
|
137
|
-
- - ">="
|
138
|
-
- !ruby/object:Gem::Version
|
139
|
-
version: '0'
|
140
|
-
- !ruby/object:Gem::Dependency
|
141
|
-
name: rubocop-performance
|
142
|
-
requirement: !ruby/object:Gem::Requirement
|
143
|
-
requirements:
|
144
|
-
- - ">="
|
145
|
-
- !ruby/object:Gem::Version
|
146
|
-
version: '0'
|
147
|
-
type: :development
|
148
|
-
prerelease: false
|
149
|
-
version_requirements: !ruby/object:Gem::Requirement
|
150
|
-
requirements:
|
151
|
-
- - ">="
|
152
|
-
- !ruby/object:Gem::Version
|
153
|
-
version: '0'
|
154
|
-
- !ruby/object:Gem::Dependency
|
155
|
-
name: rubocop-rails
|
127
|
+
name: standard
|
156
128
|
requirement: !ruby/object:Gem::Requirement
|
157
129
|
requirements:
|
158
130
|
- - ">="
|
@@ -233,6 +205,7 @@ files:
|
|
233
205
|
- lib/factory_bot/definition.rb
|
234
206
|
- lib/factory_bot/definition_hierarchy.rb
|
235
207
|
- lib/factory_bot/definition_proxy.rb
|
208
|
+
- lib/factory_bot/enum.rb
|
236
209
|
- lib/factory_bot/errors.rb
|
237
210
|
- lib/factory_bot/evaluation.rb
|
238
211
|
- lib/factory_bot/evaluator.rb
|
@@ -263,8 +236,7 @@ files:
|
|
263
236
|
homepage: https://github.com/thoughtbot/factory_bot
|
264
237
|
licenses:
|
265
238
|
- MIT
|
266
|
-
metadata:
|
267
|
-
changelog_uri: https://github.com/thoughtbot/factory_bot/blob/master/NEWS.md
|
239
|
+
metadata: {}
|
268
240
|
post_install_message:
|
269
241
|
rdoc_options: []
|
270
242
|
require_paths:
|
@@ -273,14 +245,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
273
245
|
requirements:
|
274
246
|
- - ">="
|
275
247
|
- !ruby/object:Gem::Version
|
276
|
-
version: 2.
|
248
|
+
version: 2.5.0
|
277
249
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
278
250
|
requirements:
|
279
251
|
- - ">="
|
280
252
|
- !ruby/object:Gem::Version
|
281
253
|
version: '0'
|
282
254
|
requirements: []
|
283
|
-
rubygems_version: 3.
|
255
|
+
rubygems_version: 3.1.2
|
284
256
|
signing_key:
|
285
257
|
specification_version: 4
|
286
258
|
summary: factory_bot provides a framework and DSL for defining and using model instance
|