factory_bot 4.11.1 → 6.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/CONTRIBUTING.md +58 -13
  3. data/GETTING_STARTED.md +825 -153
  4. data/LICENSE +1 -1
  5. data/NEWS.md +385 -0
  6. data/README.md +20 -30
  7. data/lib/factory_bot/aliases.rb +3 -3
  8. data/lib/factory_bot/attribute/association.rb +2 -2
  9. data/lib/factory_bot/attribute/dynamic.rb +3 -2
  10. data/lib/factory_bot/attribute.rb +4 -39
  11. data/lib/factory_bot/attribute_assigner.rb +27 -12
  12. data/lib/factory_bot/attribute_list.rb +3 -2
  13. data/lib/factory_bot/callback.rb +4 -11
  14. data/lib/factory_bot/configuration.rb +15 -19
  15. data/lib/factory_bot/declaration/association.rb +33 -3
  16. data/lib/factory_bot/declaration/dynamic.rb +3 -1
  17. data/lib/factory_bot/declaration/implicit.rb +7 -2
  18. data/lib/factory_bot/declaration.rb +5 -5
  19. data/lib/factory_bot/declaration_list.rb +3 -3
  20. data/lib/factory_bot/decorator/attribute_hash.rb +1 -1
  21. data/lib/factory_bot/decorator/invocation_tracker.rb +2 -1
  22. data/lib/factory_bot/decorator.rb +20 -4
  23. data/lib/factory_bot/definition.rb +69 -21
  24. data/lib/factory_bot/definition_hierarchy.rb +1 -11
  25. data/lib/factory_bot/definition_proxy.rb +119 -64
  26. data/lib/factory_bot/enum.rb +27 -0
  27. data/lib/factory_bot/errors.rb +7 -4
  28. data/lib/factory_bot/evaluation.rb +1 -1
  29. data/lib/factory_bot/evaluator.rb +10 -11
  30. data/lib/factory_bot/evaluator_class_definer.rb +1 -1
  31. data/lib/factory_bot/factory.rb +12 -12
  32. data/lib/factory_bot/factory_runner.rb +4 -4
  33. data/lib/factory_bot/find_definitions.rb +2 -2
  34. data/lib/factory_bot/internal.rb +91 -0
  35. data/lib/factory_bot/linter.rb +41 -28
  36. data/lib/factory_bot/null_factory.rb +13 -4
  37. data/lib/factory_bot/null_object.rb +2 -6
  38. data/lib/factory_bot/registry.rb +17 -8
  39. data/lib/factory_bot/reload.rb +2 -3
  40. data/lib/factory_bot/sequence.rb +5 -6
  41. data/lib/factory_bot/strategy/attributes_for.rb +4 -0
  42. data/lib/factory_bot/strategy/build.rb +4 -0
  43. data/lib/factory_bot/strategy/create.rb +4 -0
  44. data/lib/factory_bot/strategy/null.rb +4 -0
  45. data/lib/factory_bot/strategy/stub.rb +41 -32
  46. data/lib/factory_bot/strategy_calculator.rb +1 -1
  47. data/lib/factory_bot/strategy_syntax_method_registrar.rb +13 -2
  48. data/lib/factory_bot/syntax/default.rb +13 -25
  49. data/lib/factory_bot/syntax/methods.rb +32 -9
  50. data/lib/factory_bot/syntax.rb +2 -2
  51. data/lib/factory_bot/trait.rb +7 -4
  52. data/lib/factory_bot/version.rb +1 -1
  53. data/lib/factory_bot.rb +71 -140
  54. metadata +46 -34
  55. data/NEWS +0 -306
  56. data/lib/factory_bot/attribute/static.rb +0 -16
  57. data/lib/factory_bot/declaration/static.rb +0 -26
  58. data/lib/factory_bot/decorator/class_key_hash.rb +0 -28
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2008-2017 Joe Ferris and thoughtbot, inc.
1
+ Copyright (c) 2008-2019 Joe Ferris and thoughtbot, inc.
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  of this software and associated documentation files (the "Software"), to deal
data/NEWS.md ADDED
@@ -0,0 +1,385 @@
1
+ # News
2
+
3
+ ## 6.2.1 (March 8, 2022)
4
+ * Added: CI testing against truffleruby
5
+ * Changed: Documentation improvements for sequences and traits
6
+ * Fixed: ActiveSupport::Notifications reporting strategy through associations now report as symbols
7
+ * Fixed: `add_attribute` with reserved keywords assigns values correctly
8
+
9
+ ## 6.2.0 (May 7, 2021)
10
+ * Added: support for Ruby 3.0
11
+ * Changed: Include factory or trait name in error messages for missing traits. d05a9a3c
12
+ * Changed: Switched from Travis CI to GitHub Actions
13
+ * Fixed: More Ruby 2.7 kwarg deprecation warnings
14
+
15
+ ## 6.1.0 (July 8, 2020)
16
+ * Added: public reader for the evaluation instance, helpful for building interrelated associations
17
+ * Changed: raise a more helpful error when passing an invalid argument to an association
18
+ * Fixed: Ruby 2.7 kwarg deprecation warnings
19
+
20
+ ## 6.0.2 (June 19, 2020)
21
+ * Fixed: bug causing traits to consume more memory each time they were used
22
+
23
+ ## 6.0.1 (June 19, 2020)
24
+ * Fixed: bug with constant resolution causing unexpected uninitialized constant errors
25
+
26
+ ## 6.0.0 (June 18, 2020)
27
+ * Added: automatic definition of traits for Active Record enum attributes, enabled by default
28
+ (Note that this required changing where factory_bot constantizes the build
29
+ class, which may affect applications that were using abstract factories for
30
+ inheritance. See issue #1409.)
31
+ * Added: `traits_for_enum` method to define traits for non-Active Record enums
32
+ * Added: `build_stubbed_starting_id=` option to define the starting id for `build_stubbed`
33
+ * Removed: deprecated methods on the top-level `FactoryBot` module meant only for internal use
34
+ * Removed: support for EOL versions of Ruby (2.3, 2.4) and Rails (4.2)
35
+ * Removed: support for "abstract" factories with no associated class; use traits instead.
36
+
37
+ ## 5.2.0 (April 24, 2020)
38
+ * Added: Pass index to block for `*_list` methods
39
+ * Deprecated: methods on the top-level `FactoryBot` module meant only for internal use: `callbacks`, `configuration`, `constructor`, `initialize_with`, `register_sequence`, `resent_configuration`, `skip_create`, `to_create`
40
+
41
+ ## 5.1.2 (March 25, 2020)
42
+ * Fixed: Ruby 2.7 keyword deprecation warning in FactoryBot.lint
43
+
44
+ ## 5.1.1 (October 2, 2019)
45
+ * Improved: performance of traits
46
+ * Fixed: registering strategies on JRuby
47
+
48
+ ## 5.1.0 (September 21, 2019)
49
+ * Added: "Did you mean?" style error message to help with typos in association declarations
50
+ * Changed: `NoMethodError` for static attributes now offers a "Did you mean?" style message
51
+ * Fixed: avoid undefining inherited evaluator methods
52
+ * Fixed: avoid stubbing id for records without a primary key
53
+ * Fixed: raise a helpful error for self-referencing traits to avoid a `SystemStackError`
54
+ * Deprecated: methods on the top-level `FactoryBot` module meant only for internal use: `allow_class_lookup`, `allow_class_lookup`=, `register_trait`, `trait_by_name`, `traits`, `sequence_by_name`, `sequences`, `factory_by_name`, `register_factory`, `callback_names`, `register_callback`, `register_default_callbacks`, `register_default_strategies`, `strategies`
55
+
56
+ ## 5.0.2 (February 22, 2019)
57
+ * Bugfix: raise "Trait not registered" error when passing invalid trait arguments
58
+
59
+ ## 5.0.1 (February 15, 2019)
60
+ * Bugfix: Do not raise error when two sequences have the same name
61
+ in two traits that have the same name
62
+
63
+ ## 5.0.0 (February 1, 2019)
64
+ * Added: Verbose option to include full backtraces in the linting output
65
+ * Changed: use_parent_strategy now defaults to true, so by default the
66
+ build strategy will build, rather than create associations
67
+ * Changed: Passing a block when defining associations now raises an error
68
+ * Bugfix: use_parent_strategy is no longer reset by FactoryBot.reload
69
+ * Bugfix: rewind_sequences will now rewind local sequences along with the global ones
70
+ * Bugfix: the build_stubbed strategy now sets timestamps without changing the
71
+ the original behavior of the timestamp methods
72
+ * Bugfix: avoid a stack error when referring to an "attributes" attribute in initialize_with
73
+ * Removed: support for EOL versions of Ruby and Rails
74
+ * Removed: static attributes (use dynamic attributes with a block instead)
75
+ * Removed: looking up factories by class
76
+ * Removed: ignore method (use transient instead)
77
+ * Removed: duplicate_attribute_assignment_from_initialize_with configuration option
78
+ * Deprecated: allow_class_lookup configuration option
79
+
80
+ ## 4.11.1 (September 7, 2018)
81
+ * Documentation: Include .yardopts in the gem to fix broken RubyDoc links
82
+
83
+ ## 4.11.0 (August, 15, 2018)
84
+ * Bugfix: Do not raise error for valid build_stubbed methods: decrement, increment, and toggle
85
+ * Bugfix: Do not add timestamps with build_stubbed for objects that shouldn't have timestamps
86
+ * Deprecate static attributes
87
+
88
+ ## 4.10.0 (May 25, 2018)
89
+ * Allow sequences to be rewound
90
+
91
+ ## 4.9.0 (skipped - FactoryGirl only release)
92
+
93
+ ## 4.8.2 (October 20, 2017)
94
+ * Rename factory_girl to factory_bot
95
+
96
+ ## 4.8.1 (September 28, 2017)
97
+ * Explicitly define `#destroyed?` within the `Stub` strategy to return `nil` instead of raising
98
+ * Update various dependencies
99
+ * Update internal test suite to use RSpec's mocking/stubbing instead of mocha
100
+
101
+ ## 4.8.0 (December 16, 2016)
102
+ * Improve documentation
103
+ * Add `FactoryGirl.generate_list` to be consistent with `build_list`/`create_list` and friends
104
+ * Add `FactoryGirl.use_parent_strategy` configuration to allow associations to leverage parent build strategy
105
+
106
+ ## 4.7.0 (April 1, 2016)
107
+ * Improve documentation
108
+ * Improve instrumentation payload to include traits, overrides, and the factory itself
109
+ * Allow linting of traits
110
+ * Deprecate factory lookup by class name in preparation for 5.0
111
+ * Improve internal performance by using flat_map instead of map and compact
112
+ * Improve handling of dirty attributes after building a stubbed object
113
+ * Reduce warnings from redefining methods
114
+
115
+ ## 4.6.0 (skipped)
116
+
117
+ ## 4.5.0 (October 17, 2014)
118
+ * Improve FactoryGirl.lint by including exception and message in output
119
+ * Allow selective linting
120
+ * Use more explicit #public_send when doing attribute assignment
121
+ * Improve documentation around FactoryGirl.lint and initialize_with
122
+ * Deprecate #ignore in favor of #transient
123
+
124
+ ## 4.4.0 (February 10, 2014)
125
+ * Add FactoryGirl.lint
126
+ * Fix memory leak in duplicate traits
127
+ * Update documentation
128
+
129
+ ## 4.3.0 (November 3, 2013)
130
+ * Start testing against Rails 4.0 and Ruby 2.0.0
131
+ * Stop testing against Rails 3.0 and Ruby 1.9.2
132
+ * Add `*_pair` methods to only build two objects
133
+ * Raise if a method is defined with a FactoryGirl block (factory or trait)
134
+ * Allow use of Symbol#to_proc in callbacks
135
+ * Add global callbacks
136
+ * Improve GETTING_STARTED and README
137
+
138
+ ## 4.2.0 (January 18, 2013)
139
+ * Improve documentation
140
+ * Allow `*_list` syntax methods to accept a block
141
+ * Update gem dependencies
142
+ * Allow setting id for objects created with `build_stubbed`
143
+ * Fix Stub strategy to mimic ActiveRecord regarding `created_at`
144
+ * Evaluate sequences within the context of an Evaluator
145
+ * Fix Mocha deprecation warning
146
+ * Fix some warnings when running RUBYOPT=-w rake
147
+ * Convert test suite to RSpec's "expect" syntax
148
+
149
+ ## 4.1.0 (September 11, 2012)
150
+ * Allow multiple callbacks to bind to the same block
151
+ * Fix documentation surrounding the stub strategy
152
+
153
+ ## 4.0.0 (August 3, 2012)
154
+ * Remove deprecated cucumber_steps
155
+ * Remove deprecated alternate syntaxes
156
+ * Deprecate duplicate_attribute_assignment_from_initialize_with, which is now unused
157
+ as attributes assigned within initialize_with are not subsequently assigned
158
+
159
+ ## 3.6.1 (August 2, 2012)
160
+ Update README to include info about running with JRuby
161
+ * Update dependencies on RSpec and tiny versions of Rails in Appraisal
162
+ * Improve flexibility of using traits with associations and add documentation
163
+ * Stub update_column to raise to mirror ActiveRecord's change from update_attribute
164
+
165
+ ## 3.6.0 (July 27, 2012)
166
+ * Code/spec cleanup
167
+ * Allow factories with traits to be used in associations
168
+ * Refactor Factory to use DefinitionHierarchy to handle managing callbacks,
169
+ custom constructor, and custom to_create
170
+ * Add memoization to speed up factories providing attribute overrides
171
+ * Add initial support of JRuby when running in 1.9 mode
172
+ * Improve docs on what happens when including FactoryGirl::Syntax::Methods
173
+
174
+ ## 3.5.0 (June 22, 2012)
175
+ * Allow created_at to be set when using build_stubbed
176
+ * Deprecate FactoryGirl step definitions
177
+
178
+ ## 3.4.2 (June 19, 2012)
179
+ * Fix bug in traits with callbacks called implicitly in factories whose
180
+ callbacks trigger multiple times
181
+
182
+ ## 3.4.1 (June 18, 2012)
183
+ * Fix traits so they can be nested and referred to from other traits
184
+
185
+ ## 3.4.0 (June 11, 2012)
186
+ * Sequences support Enumerators
187
+ * Optionally disable duplicate assignment of attributes in initialize_with
188
+ * Make hash of public attributes available in initialize_with
189
+ * Support referring to a factory based on class name
190
+
191
+ ## 3.3.0 (May 13, 2012)
192
+ * Allow to_create, skip_create, and initialize_with to be defined globally
193
+ * Allow to_create, skip_create, and initialize_with to be defined within traits
194
+ * Fix deprecation messages for alternate syntaxes (make, generate, etc.)
195
+ * Improve library documentation
196
+ * Deprecate after_build, after_create, before_create, after_stub in favor of new callbacks
197
+ * Introduce new callback syntax: after(:build) {}, after(:custom) {}, or callback(:different) {}
198
+ This allows for declaring any callback, usable with custom strategies
199
+ * Add attributes_for_list and build_stubbed_list with the StrategySyntaxMethodRegistrar
200
+ * Allow use of syntax methods (build, create, generate, etc) implicitly in callbacks
201
+ * Internal refactoring of a handful of components
202
+
203
+ ## 3.2.0 (April 24, 2012)
204
+ * Use AS::Notifications for pub/sub to track running factories
205
+ * Call new within initialize_with implicitly on the build class
206
+ * Skip to_create with skip_create
207
+ * Allow registration of custom strategies
208
+ * Deprecate alternate syntaxes
209
+ * Implicitly call factory_bot's syntax methods from dynamic attributes
210
+
211
+ ## 3.1.0 (April 6, 2012)
212
+ * Sequences support aliases, which reference the same block
213
+ * Update documentation
214
+ * Add before_create callback
215
+ * Support use of #attribute_names method to determine available attributes for steps
216
+ * Use ActiveSupport::Deprecation for all deprecations
217
+
218
+ ## 3.0.0 (March 23, 2012)
219
+ * Deprecate the vintage syntax
220
+ * Remove Rails 2.x support
221
+ * Remove Ruby 1.8 support
222
+ * Remove deprecated features, including default_strategy, factory_name,
223
+ :method for defining default strategy, ignore on individual attributes, and
224
+ interacting with Factory the way you would FactoryGirl
225
+
226
+ ## 2.6.4 (March 16, 2012)
227
+ * Do not ignore names of transient attributes
228
+ * Ensure attributes set on instance are calculated uniquely
229
+
230
+ ## 2.6.3 (March 9, 2012)
231
+ * Fix issue with traits not being present the first time a factory is accessed
232
+ * Update available Cucumber step definitions to not require a trailing colon
233
+ when building a table of attributes to instantiate records with
234
+
235
+ ## 2.6.2 (March 9, 2012)
236
+ * Allow factories to use all their ancestors' traits
237
+ * Ignore bin dir generated by bundler
238
+ * Namespace ::Factory as top-level to fix vintage syntax issue with
239
+ Ruby 1.9.2-p3p18
240
+
241
+ ## 2.6.1 (March 2, 2012)
242
+ * Use FactoryGirl.reload in specs
243
+ * Clean up running named factories with a particular strategy with
244
+ FactoryGirl::FactoryRunner
245
+
246
+ ## 2.6.0 (February 17, 2012)
247
+ * Improve documentation of has_many associations in the GETTING_STARTED
248
+ document
249
+ * Deprecate :method in favor of :strategy when overriding an association's
250
+ build strategy
251
+
252
+ ## 2.5.2 (February 10, 2012)
253
+ * Fix step definitions to use associations defined in parent factories
254
+ * Add inline trait support to (build|create)_list
255
+ * Update ActiveSupport dependency to >= 2.3.9, which introduced
256
+ class_attribute
257
+
258
+ ## 2.5.1 (February 3, 2012)
259
+ * Fix attribute evaluation when the attribute isn't defined in the factory but
260
+ is a private method on Object
261
+ * Update rubygems on Travis before running tests
262
+ * Fix spec name
263
+ * Update GETTING_STARTED with correct usage of build_stubbed
264
+ * Update README with more info on initialize_with
265
+ * Honor :parent on factory over block nesting
266
+
267
+ ## 2.5.0 (January 20, 2012)
268
+ * Revert 'Deprecate build_stubbed and attributes_for'
269
+ * Implement initialize_with to allow overriding object instantiation
270
+ * Ensure FG runs against Rails 3.2.0
271
+
272
+ ## 2.4.2 (January 18, 2012)
273
+ * Fix inline traits' interaction with defaults on the factory
274
+
275
+ ## 2.4.1 (January 17, 2012)
276
+ * Deprecate build_stubbed and attributes_for
277
+ * Fix inline traits
278
+
279
+ ## 2.4.0 (January 13, 2012)
280
+ * Refactor internals of FactoryGirl to use anonymous class on which attributes
281
+ get defined
282
+ * Explicitly require Ruby 1.8.7 or higher in gemspec
283
+ * Fix documentation
284
+ * Add Gemnasium status to documentation
285
+ * Supplying a Class to a factory that overrides to_s no longer results in
286
+ getting the wrong Class constructed
287
+ * Be more agnostic about ORMs when using columns in FactoryGirl step
288
+ definitions
289
+ * Test against Active Record 3.2.0.rc2
290
+ * Update GETTING_STARTED to use Ruby syntax highlighting
291
+
292
+ ## 2.3.2 (November 26, 2011)
293
+ * Move logic of where instance.save! is set to Definition
294
+ * Fix method name from aliases_for? to alias_for?
295
+ * Refactor internal attribute handling to use an anonymous class instead of
296
+ faking Ruby's variable resolution. This allows for more sane usage of
297
+ attributes without having to manage sorting priority because attributes
298
+ can turn themselves into procs, which are used with define_method on a
299
+ class so attributes work correctly all the time.
300
+
301
+ ## 2.3.1 (November 23, 2011)
302
+ * Remove internally-used associate method from all the FactoryGirl::Proxy subclasses
303
+ * Move around requiring of files
304
+ * Consolidate errors into factory_bot.rb
305
+ * Refactor AttributeList to deal with priority only when iterating over
306
+ attributes
307
+ * Refactor internals of some of the Proxy subclasses
308
+ * Ensure callbacks on traits are executed in the correct order
309
+
310
+ ## 2.3.0 (November 18, 2011)
311
+ * Registries are named, resulting in better messages when factories, traits,
312
+ or sequences cannot be found
313
+ * Fix incorrect tests
314
+ * Internals refactoring introducing FactoryGirl::NullFactory,
315
+ FactoryGirl::Definition, and FactoryGirl::DeclarationList
316
+ * Use ActiveSupport for Hash#except and its delegation capabilities
317
+ * Fix usage of callbacks when added via implicit traits
318
+ * Use Bundler tasks and clean up dependencies
319
+ * Fix failing spec for big letters in factory name passed as symbol
320
+ * Add ability for traits to be added dynamically when creating an instance via
321
+ build, create, build_stubbed, or attributes_for
322
+
323
+ ## 2.2.0 (October 14, 2011)
324
+ * Clean up RSpec suite to not use 'should'
325
+ * Use create_list in step definitions
326
+ * Syntax methods that deal with ORM interaction (attributes_for, build, build_stubbed,
327
+ and create) now accept a block that yields the result. This results in a
328
+ more convenient way to interact with the result than using Object.tap.
329
+ * Standardize deprecation warnings
330
+ * Update transient attribute syntax to use blocks instead of calling ignore on
331
+ each attribute declaration
332
+ * Parents can be defined after children because factories are evaluated when
333
+ they're used; this means breaking up factories across multiple files will
334
+ behave as expected
335
+ * Large internal refactoring, including changing access modifiers for a
336
+ handful of methods for a more clearly defined API
337
+
338
+ ## 2.1.2 (September 23, 2011)
339
+ * Bugfix: Vintage syntax fixed after bug introduced in 2.1.1
340
+ * Introduce dependency on activesupport to remove code from Factory class
341
+
342
+ ## 2.1.1 (September 23, 2011) (yanked)
343
+ * Bugfix: Parent object callbacks are run before child object callbacks
344
+ * Declarations: allow overriding/modification of individual traits in child factories
345
+ * Callbacks refactored to not be attributes
346
+ * Updating documentation for formatting and clarity (incl. new specificity for cucumber)
347
+
348
+ ## 2.1.0 (September 02, 2011)
349
+ * Bugfix: created_at now defined for stubbed models
350
+ * Gemspec updated for use with Rails 3.1
351
+ * Factories can now be modified post-definition (useful for overriding defaults from gems/plugins)
352
+ * All factories can now be reloaded with Factory.reload
353
+ * Add :method => build to factory associations to prevent saving of associated objects
354
+ * Factories defined in {Rails.root}/factories are now loaded by default
355
+ * Various documentation updates
356
+
357
+ ## 1.1.4 (November 28, 2008)
358
+ * Factory.build now uses Factory.create for associations of the built object
359
+ * Factory definitions are now detected in subdirectories, such as
360
+ factories/person_factory.rb (thanks to Josh Nichols)
361
+ * Factory definitions are now loaded after the environment in a Rails project
362
+ (fixes some issues with dependencies being loaded too early) (thanks to
363
+ Josh Nichols)
364
+ * Factory names ending in 's' no longer cause problems (thanks to Alex Sharp
365
+ and Josh Owens)
366
+
367
+ ## 1.1.3 (September 12, 2008)
368
+ * Automatically pull in definitions from factories.rb, test/factories.rb, or
369
+ spec/factories.rb
370
+ ## 1.1.2 (July 30, 2008)
371
+ * Improved error handling for invalid and undefined factories/attributes
372
+ * Improved handling of strings vs symbols vs classes
373
+ * Added a prettier syntax for handling associations
374
+ * Updated documentation and fixed compatibility with Rails 2.1
375
+
376
+ ## 1.1.1 (June 23, 2008)
377
+ * The attribute "name" no longer requires using #add_attribute
378
+
379
+ ## 1.1.0 (June 03, 2008)
380
+ * Added support for dependent attributes
381
+ * Fixed the attributes_for build strategy to not build associations
382
+ * Added support for sequences
383
+
384
+ ## 1.0.0 (May 31, 2008)
385
+ * First version
data/README.md CHANGED
@@ -1,11 +1,11 @@
1
- # factory_bot [![Build Status][ci-image]][ci] [![Code Climate][grade-image]][grade] [![Gem Version][version-image]][version] [![Reviewed by Hound][hound-badge-image]][hound]
1
+ # factory_bot [![Build Status][ci-image]][ci] [![Code Climate][grade-image]][grade] [![Gem Version][version-image]][version]
2
2
 
3
3
  factory_bot is a fixtures replacement with a straightforward definition syntax, support for multiple build strategies (saved instances, unsaved instances, attribute hashes, and stubbed objects), and support for multiple factories for the same class (user, admin_user, and so on), including factory inheritance.
4
4
 
5
5
  If you want to use factory_bot with Rails, see
6
6
  [factory_bot_rails](https://github.com/thoughtbot/factory_bot_rails).
7
7
 
8
- _[Interested in the history of the project name?](NAME.md)_
8
+ _[Interested in the history of the project name?][NAME]_
9
9
 
10
10
 
11
11
  ### Transitioning from factory\_girl?
@@ -21,7 +21,7 @@ You should find the documentation for your version of factory_bot on [Rubygems](
21
21
  See [GETTING_STARTED] for information on defining and using factories. We also
22
22
  have [a detailed introductory video][], available for free on Upcase.
23
23
 
24
- [a detailed introductory video]: https://upcase.com/videos/factory-girl?utm_source=github&utm_medium=open-source&utm_campaign=factory-girl
24
+ [a detailed introductory video]: https://upcase.com/videos/factory-bot?utm_source=github&utm_medium=open-source&utm_campaign=factory-girl
25
25
 
26
26
  Install
27
27
  --------
@@ -40,27 +40,10 @@ To install the gem manually from your shell, run:
40
40
  gem install factory_bot
41
41
  ```
42
42
 
43
- **Caveat:** As of ActiveSupport 5.0 and above, Ruby 2.2.2+ is required. Because
44
- of Rubygems' dependency resolution when installing gems, you may see an error
45
- similar to:
46
-
47
- ```
48
- $ gem install factory_bot
49
- ERROR: Error installing factory_bot:
50
- activesupport requires Ruby version >= 2.2.2.
51
- ```
52
-
53
- To bypass this, install a pre-5.0 version of ActiveSupport before installing
54
- manually.
55
-
56
43
  Supported Ruby versions
57
44
  -----------------------
58
45
 
59
- The factory_bot 3.x+ series supports MRI Ruby 1.9. Additionally, factory_bot
60
- 3.6+ supports JRuby 1.6.7.2+ while running in 1.9 mode. See [GETTING_STARTED]
61
- for more information on configuring the JRuby environment.
62
-
63
- For versions of Ruby prior to 1.9, please use factory_bot 2.x.
46
+ Supported Ruby versions are listed in [`.github/workflows/build.yml`](https://github.com/thoughtbot/factory_bot/blob/master/.github/workflows/build.yml)
64
47
 
65
48
  More Information
66
49
  ----------------
@@ -70,30 +53,37 @@ More Information
70
53
  * [Issues](https://github.com/thoughtbot/factory_bot/issues)
71
54
  * [GIANT ROBOTS SMASHING INTO OTHER GIANT ROBOTS](https://robots.thoughtbot.com/)
72
55
 
73
- You may also find useful information under the [factory_girl tag on Stack Overflow](https://stackoverflow.com/questions/tagged/factory-girl).
56
+ [GETTING_STARTED]: https://github.com/thoughtbot/factory_bot/blob/master/GETTING_STARTED.md
57
+ [NAME]: https://github.com/thoughtbot/factory_bot/blob/master/NAME.md
58
+
59
+ Useful Tools
60
+ ------------
74
61
 
75
- [GETTING_STARTED]: https://www.rubydoc.info/gems/factory_bot/file/GETTING_STARTED.md
62
+ * [FactoryTrace](https://github.com/djezzzl/factory_trace) - helps to find unused factories and traits.
76
63
 
77
64
  Contributing
78
65
  ------------
79
66
 
80
67
  Please see [CONTRIBUTING.md](https://github.com/thoughtbot/factory_bot/blob/master/CONTRIBUTING.md).
81
68
 
82
- factory_bot was originally written by Joe Ferris and is now maintained by Josh
83
- Clayton. Many improvements and bugfixes were contributed by the [open source
69
+ factory_bot was originally written by Joe Ferris and is maintained by thoughtbot.
70
+ Many improvements and bugfixes were contributed by the [open source
84
71
  community](https://github.com/thoughtbot/factory_bot/graphs/contributors).
85
72
 
86
73
  License
87
74
  -------
88
75
 
89
- factory_bot is Copyright © 2008-2016 Joe Ferris and thoughtbot. It is free
76
+ factory_bot is Copyright © 2008-2020 Joe Ferris and thoughtbot. It is free
90
77
  software, and may be redistributed under the terms specified in the
91
- [LICENSE](/LICENSE) file.
78
+ [LICENSE] file.
79
+
80
+ [LICENSE]: https://github.com/thoughtbot/factory_bot/blob/master/LICENSE
81
+
92
82
 
93
83
  About thoughtbot
94
84
  ----------------
95
85
 
96
- ![thoughtbot](https://presskit.thoughtbot.com/images/thoughtbot-logo-for-readmes.svg)
86
+ ![thoughtbot](https://thoughtbot.com/brand_assets/93:44.svg)
97
87
 
98
88
  factory_bot is maintained and funded by thoughtbot, inc.
99
89
  The names and logos for thoughtbot are trademarks of thoughtbot, inc.
@@ -104,8 +94,8 @@ See [our other projects][community] or
104
94
 
105
95
  [community]: https://thoughtbot.com/community?utm_source=github
106
96
  [hire]: https://thoughtbot.com/hire-us?utm_source=github
107
- [ci-image]: https://travis-ci.org/thoughtbot/factory_bot.svg
108
- [ci]: https://travis-ci.org/thoughtbot/factory_bot?branch=master
97
+ [ci-image]: https://github.com/thoughtbot/factory_bot/actions/workflows/build.yml/badge.svg
98
+ [ci]: https://github.com/thoughtbot/factory_bot/actions?query=workflow%3A.github%2Fworkflows%2Fbuild.yml+branch%3Amaster++
109
99
  [grade-image]: https://codeclimate.com/github/thoughtbot/factory_bot/badges/gpa.svg
110
100
  [grade]: https://codeclimate.com/github/thoughtbot/factory_bot
111
101
  [version-image]: https://badge.fury.io/rb/factory_bot.svg
@@ -9,10 +9,10 @@ module FactoryBot
9
9
  ]
10
10
 
11
11
  def self.aliases_for(attribute)
12
- aliases.map do |(pattern, replace)|
13
- if pattern.match(attribute.to_s)
12
+ aliases.map { |(pattern, replace)|
13
+ if pattern.match?(attribute)
14
14
  attribute.to_s.sub(pattern, replace).to_sym
15
15
  end
16
- end.compact << attribute
16
+ }.compact << attribute
17
17
  end
18
18
  end
@@ -6,12 +6,12 @@ module FactoryBot
6
6
 
7
7
  def initialize(name, factory, overrides)
8
8
  super(name, false)
9
- @factory = factory
9
+ @factory = factory
10
10
  @overrides = overrides
11
11
  end
12
12
 
13
13
  def to_proc
14
- factory = @factory
14
+ factory = @factory
15
15
  overrides = @overrides
16
16
  traits_and_overrides = [factory, overrides].flatten
17
17
  factory_name = traits_and_overrides.shift
@@ -12,10 +12,11 @@ module FactoryBot
12
12
 
13
13
  -> {
14
14
  value = case block.arity
15
- when 1, -1 then instance_exec(self, &block)
15
+ when 1, -1, -2 then instance_exec(self, &block)
16
16
  else instance_exec(&block)
17
- end
17
+ end
18
18
  raise SequenceAbuseError if FactoryBot::Sequence === value
19
+
19
20
  value
20
21
  }
21
22
  end
@@ -1,7 +1,6 @@
1
- require 'factory_bot/attribute/static'
2
- require 'factory_bot/attribute/dynamic'
3
- require 'factory_bot/attribute/association'
4
- require 'factory_bot/attribute/sequence'
1
+ require "factory_bot/attribute/dynamic"
2
+ require "factory_bot/attribute/association"
3
+ require "factory_bot/attribute/sequence"
5
4
 
6
5
  module FactoryBot
7
6
  # @api private
@@ -11,11 +10,10 @@ module FactoryBot
11
10
  def initialize(name, ignored)
12
11
  @name = name.to_sym
13
12
  @ignored = ignored
14
- ensure_non_attribute_writer!
15
13
  end
16
14
 
17
15
  def to_proc
18
- -> { }
16
+ -> {}
19
17
  end
20
18
 
21
19
  def association?
@@ -25,38 +23,5 @@ module FactoryBot
25
23
  def alias_for?(attr)
26
24
  FactoryBot.aliases_for(attr).include?(name)
27
25
  end
28
-
29
- private
30
-
31
- def ensure_non_attribute_writer!
32
- NonAttributeWriterValidator.new(@name).validate!
33
- end
34
-
35
- class NonAttributeWriterValidator
36
- def initialize(method_name)
37
- @method_name = method_name.to_s
38
- @method_name_setter_match = @method_name.match(/(.*)=$/)
39
- end
40
-
41
- def validate!
42
- if method_is_writer?
43
- raise AttributeDefinitionError, error_message
44
- end
45
- end
46
-
47
- private
48
-
49
- def method_is_writer?
50
- !!@method_name_setter_match
51
- end
52
-
53
- def attribute_name
54
- @method_name_setter_match[1]
55
- end
56
-
57
- def error_message
58
- "factory_bot uses '#{attribute_name} value' syntax rather than '#{attribute_name} = value'"
59
- end
60
- end
61
26
  end
62
27
  end
@@ -3,9 +3,9 @@ module FactoryBot
3
3
  class AttributeAssigner
4
4
  def initialize(evaluator, build_class, &instance_builder)
5
5
  @build_class = build_class
6
- @instance_builder = instance_builder
7
- @evaluator = evaluator
8
- @attribute_list = evaluator.class.attribute_list
6
+ @instance_builder = instance_builder
7
+ @evaluator = evaluator
8
+ @attribute_list = evaluator.class.attribute_list
9
9
  @attribute_names_assigned = []
10
10
  end
11
11
 
@@ -22,21 +22,24 @@ module FactoryBot
22
22
  def hash
23
23
  @evaluator.instance = build_hash
24
24
 
25
- attributes_to_set_on_hash.inject({}) do |result, attribute|
25
+ attributes_to_set_on_hash.each_with_object({}) do |attribute, result|
26
26
  result[attribute] = get(attribute)
27
- result
28
27
  end
29
28
  end
30
29
 
31
30
  private
32
31
 
33
32
  def method_tracking_evaluator
34
- @method_tracking_evaluator ||= Decorator::AttributeHash.new(decorated_evaluator, attribute_names_to_assign)
33
+ @method_tracking_evaluator ||= Decorator::AttributeHash.new(
34
+ decorated_evaluator,
35
+ attribute_names_to_assign
36
+ )
35
37
  end
36
38
 
37
39
  def decorated_evaluator
38
- Decorator::InvocationTracker.new(
39
- Decorator::NewConstructor.new(@evaluator, @build_class)
40
+ Decorator::NewConstructor.new(
41
+ Decorator::InvocationTracker.new(@evaluator),
42
+ @build_class
40
43
  )
41
44
  end
42
45
 
@@ -65,7 +68,11 @@ module FactoryBot
65
68
  end
66
69
 
67
70
  def attribute_names_to_assign
68
- @attribute_names_to_assign ||= non_ignored_attribute_names + override_names - ignored_attribute_names - alias_names_to_ignore
71
+ @attribute_names_to_assign ||=
72
+ non_ignored_attribute_names +
73
+ override_names -
74
+ ignored_attribute_names -
75
+ alias_names_to_ignore
69
76
  end
70
77
 
71
78
  def non_ignored_attribute_names
@@ -89,9 +96,17 @@ module FactoryBot
89
96
  end
90
97
 
91
98
  def alias_names_to_ignore
92
- @attribute_list.non_ignored.flat_map do |attribute|
93
- override_names.map { |override| attribute.name if attribute.alias_for?(override) && attribute.name != override && !ignored_attribute_names.include?(override) }
94
- end.compact
99
+ @attribute_list.non_ignored.flat_map { |attribute|
100
+ override_names.map do |override|
101
+ attribute.name if ignorable_alias?(attribute, override)
102
+ end
103
+ }.compact
104
+ end
105
+
106
+ def ignorable_alias?(attribute, override)
107
+ attribute.alias_for?(override) &&
108
+ attribute.name != override &&
109
+ !ignored_attribute_names.include?(override)
95
110
  end
96
111
  end
97
112
  end