factory_girl 4.5.0 → 4.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +25 -5
- data/Appraisals +11 -7
- data/CONTRIBUTING.md +44 -6
- data/GETTING_STARTED.md +175 -59
- data/Gemfile +5 -0
- data/Gemfile.lock +86 -63
- data/LICENSE +1 -1
- data/NEWS +12 -1
- data/README.md +21 -10
- data/Rakefile +1 -3
- data/factory_girl.gemspec +6 -14
- data/gemfiles/3.2.gemfile +4 -1
- data/gemfiles/3.2.gemfile.lock +84 -52
- data/gemfiles/4.0.gemfile +4 -1
- data/gemfiles/4.0.gemfile.lock +75 -49
- data/gemfiles/4.1.gemfile +4 -1
- data/gemfiles/4.1.gemfile.lock +78 -48
- data/gemfiles/4.2.gemfile +10 -0
- data/gemfiles/4.2.gemfile.lock +123 -0
- data/gemfiles/5.0.gemfile +10 -0
- data/gemfiles/5.0.gemfile.lock +126 -0
- data/lib/factory_girl.rb +13 -4
- data/lib/factory_girl/attribute/dynamic.rb +4 -1
- data/lib/factory_girl/attribute_assigner.rb +2 -2
- data/lib/factory_girl/configuration.rb +4 -0
- data/lib/factory_girl/decorator.rb +2 -2
- data/lib/factory_girl/decorator/class_key_hash.rb +2 -1
- data/lib/factory_girl/definition_proxy.rb +2 -2
- data/lib/factory_girl/evaluator.rb +4 -0
- data/lib/factory_girl/factory_runner.rb +7 -1
- data/lib/factory_girl/find_definitions.rb +1 -1
- data/lib/factory_girl/linter.rb +65 -14
- data/lib/factory_girl/strategy/stub.rb +25 -1
- data/lib/factory_girl/strategy_syntax_method_registrar.rb +4 -0
- data/lib/factory_girl/version.rb +1 -1
- metadata +26 -185
- data/features/find_definitions.feature +0 -75
- data/features/step_definitions/database_steps.rb +0 -9
- data/features/step_definitions/factory_girl_steps.rb +0 -36
- data/features/support/env.rb +0 -10
- data/features/support/factories.rb +0 -16
- data/gemfiles/3.1.gemfile +0 -7
- data/gemfiles/3.1.gemfile.lock +0 -91
- data/spec/acceptance/activesupport_instrumentation_spec.rb +0 -62
- data/spec/acceptance/aliases_spec.rb +0 -19
- data/spec/acceptance/attribute_aliases_spec.rb +0 -45
- data/spec/acceptance/attribute_existing_on_object_spec.rb +0 -68
- data/spec/acceptance/attributes_for_spec.rb +0 -97
- data/spec/acceptance/attributes_from_instance_spec.rb +0 -53
- data/spec/acceptance/attributes_ordered_spec.rb +0 -51
- data/spec/acceptance/build_list_spec.rb +0 -56
- data/spec/acceptance/build_spec.rb +0 -86
- data/spec/acceptance/build_stubbed_spec.rb +0 -161
- data/spec/acceptance/callbacks_spec.rb +0 -253
- data/spec/acceptance/create_list_spec.rb +0 -97
- data/spec/acceptance/create_pair_spec.rb +0 -32
- data/spec/acceptance/create_spec.rb +0 -116
- data/spec/acceptance/define_child_before_parent_spec.rb +0 -21
- data/spec/acceptance/defining_methods_inside_a_factory_spec.rb +0 -17
- data/spec/acceptance/definition_camel_string_spec.rb +0 -15
- data/spec/acceptance/definition_spec.rb +0 -21
- data/spec/acceptance/definition_without_block_spec.rb +0 -15
- data/spec/acceptance/global_initialize_with_spec.rb +0 -82
- data/spec/acceptance/global_to_create_spec.rb +0 -122
- data/spec/acceptance/initialize_with_spec.rb +0 -217
- data/spec/acceptance/keyed_by_class_spec.rb +0 -22
- data/spec/acceptance/lint_spec.rb +0 -65
- data/spec/acceptance/modify_factories_spec.rb +0 -184
- data/spec/acceptance/modify_inherited_spec.rb +0 -52
- data/spec/acceptance/nested_attributes_spec.rb +0 -32
- data/spec/acceptance/overrides_spec.rb +0 -61
- data/spec/acceptance/parent_spec.rb +0 -90
- data/spec/acceptance/private_attributes_spec.rb +0 -20
- data/spec/acceptance/register_strategies_spec.rb +0 -136
- data/spec/acceptance/sequence_context_spec.rb +0 -52
- data/spec/acceptance/sequence_spec.rb +0 -61
- data/spec/acceptance/skip_create_spec.rb +0 -19
- data/spec/acceptance/stub_spec.rb +0 -62
- data/spec/acceptance/syntax_methods_within_dynamic_attributes_spec.rb +0 -51
- data/spec/acceptance/traits_spec.rb +0 -727
- data/spec/acceptance/transient_attributes_spec.rb +0 -151
- data/spec/factory_girl/aliases_spec.rb +0 -31
- data/spec/factory_girl/attribute/association_spec.rb +0 -28
- data/spec/factory_girl/attribute/dynamic_spec.rb +0 -52
- data/spec/factory_girl/attribute/sequence_spec.rb +0 -16
- data/spec/factory_girl/attribute/static_spec.rb +0 -19
- data/spec/factory_girl/attribute_list_spec.rb +0 -143
- data/spec/factory_girl/attribute_spec.rb +0 -16
- data/spec/factory_girl/callback_spec.rb +0 -41
- data/spec/factory_girl/declaration/implicit_spec.rb +0 -25
- data/spec/factory_girl/declaration_list_spec.rb +0 -67
- data/spec/factory_girl/definition_proxy_spec.rb +0 -215
- data/spec/factory_girl/definition_spec.rb +0 -64
- data/spec/factory_girl/disallows_duplicates_registry_spec.rb +0 -19
- data/spec/factory_girl/evaluator_class_definer_spec.rb +0 -77
- data/spec/factory_girl/factory_spec.rb +0 -259
- data/spec/factory_girl/find_definitions_spec.rb +0 -110
- data/spec/factory_girl/null_factory_spec.rb +0 -13
- data/spec/factory_girl/null_object_spec.rb +0 -22
- data/spec/factory_girl/registry_spec.rb +0 -68
- data/spec/factory_girl/sequence_spec.rb +0 -95
- data/spec/factory_girl/strategy/attributes_for_spec.rb +0 -18
- data/spec/factory_girl/strategy/build_spec.rb +0 -7
- data/spec/factory_girl/strategy/create_spec.rb +0 -25
- data/spec/factory_girl/strategy/stub_spec.rb +0 -38
- data/spec/factory_girl/strategy_calculator_spec.rb +0 -29
- data/spec/factory_girl_spec.rb +0 -22
- data/spec/spec_helper.rb +0 -26
- data/spec/support/macros/define_constant.rb +0 -86
- data/spec/support/matchers/callback.rb +0 -9
- data/spec/support/matchers/declaration.rb +0 -83
- data/spec/support/matchers/delegate.rb +0 -44
- data/spec/support/matchers/trait.rb +0 -11
- data/spec/support/shared_examples/strategy.rb +0 -90
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: fa9eb22350ba005f4e5f9a78f10e5dd825035c21
|
|
4
|
+
data.tar.gz: 2dcd957723bd03f85b6dbc4a674f031873a29a90
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c3b3e8264c3284d727289be55a71a8f641b801e5610f9261e3caa5d35894fba887d8d23e518e61562751ea25d4d2fcb4a704a0d2132d18e7a0f5fadfedc474df
|
|
7
|
+
data.tar.gz: 2bab54b8012900039840a4259c4c5cb98378a312f899530825b4743de4bb1c9d344920cb1073a63e057b7e99b19e06c75c001d1f53fb7ba3c559310b48f9b42b
|
data/.travis.yml
CHANGED
|
@@ -2,18 +2,38 @@ rvm:
|
|
|
2
2
|
- 1.9.3
|
|
3
3
|
- 2.0.0
|
|
4
4
|
- 2.1.0
|
|
5
|
+
- 2.2.2
|
|
6
|
+
- 2.3.0
|
|
5
7
|
- jruby-19mode
|
|
8
|
+
- rbx-2
|
|
6
9
|
before_install:
|
|
7
10
|
- gem update --system
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
- gem update bundler
|
|
12
|
+
- gem cleanup bundler
|
|
13
|
+
install: "bundle install --jobs 8"
|
|
14
|
+
script: "bundle exec rake"
|
|
12
15
|
gemfile:
|
|
13
|
-
- gemfiles/3.1.gemfile
|
|
14
16
|
- gemfiles/3.2.gemfile
|
|
15
17
|
- gemfiles/4.0.gemfile
|
|
16
18
|
- gemfiles/4.1.gemfile
|
|
19
|
+
- gemfiles/4.2.gemfile
|
|
20
|
+
- gemfiles/5.0.gemfile
|
|
21
|
+
matrix:
|
|
22
|
+
fast_finish: true
|
|
23
|
+
allow_failures:
|
|
24
|
+
- rvm: jruby-19mode
|
|
25
|
+
gemfile: gemfiles/5.0.gemfile
|
|
26
|
+
- rvm: 1.9.3
|
|
27
|
+
gemfile: gemfiles/5.0.gemfile
|
|
28
|
+
- rvm: 2.0.0
|
|
29
|
+
gemfile: gemfiles/5.0.gemfile
|
|
30
|
+
- rvm: 2.1.0
|
|
31
|
+
gemfile: gemfiles/5.0.gemfile
|
|
32
|
+
- rvm: rbx-2
|
|
33
|
+
gemfile: gemfiles/5.0.gemfile
|
|
17
34
|
branches:
|
|
18
35
|
only:
|
|
19
36
|
- master
|
|
37
|
+
|
|
38
|
+
sudo: false
|
|
39
|
+
cache: bundler
|
data/Appraisals
CHANGED
|
@@ -1,15 +1,19 @@
|
|
|
1
|
-
appraise "3.1" do
|
|
2
|
-
gem "activerecord", "~> 3.1.12"
|
|
3
|
-
end
|
|
4
|
-
|
|
5
1
|
appraise "3.2" do
|
|
6
|
-
gem "activerecord",
|
|
2
|
+
gem "activerecord", git: 'https://github.com/rails/rails.git', branch: '3-2-stable'
|
|
7
3
|
end
|
|
8
4
|
|
|
9
5
|
appraise '4.0' do
|
|
10
|
-
gem 'activerecord', "~> 4.0.
|
|
6
|
+
gem 'activerecord', "~> 4.0.13"
|
|
11
7
|
end
|
|
12
8
|
|
|
13
9
|
appraise '4.1' do
|
|
14
|
-
gem 'activerecord', "~> 4.1.
|
|
10
|
+
gem 'activerecord', "~> 4.1.14"
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
appraise '4.2' do
|
|
14
|
+
gem 'activerecord', "~> 4.2.5.1"
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
appraise '5.0' do
|
|
18
|
+
gem 'activerecord', "~> 5.0.0.beta2"
|
|
15
19
|
end
|
data/CONTRIBUTING.md
CHANGED
|
@@ -1,10 +1,48 @@
|
|
|
1
|
-
Contributing to
|
|
1
|
+
# Contributing to Factory Girl
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
3. Send a pull request.
|
|
3
|
+
We love pull requests from everyone. By participating in this project, you
|
|
4
|
+
agree to abide by the thoughtbot [code of conduct].
|
|
6
5
|
|
|
7
|
-
|
|
6
|
+
[code of conduct]: https://thoughtbot.com/open-source-code-of-conduct
|
|
8
7
|
|
|
9
|
-
|
|
8
|
+
Here are some ways *you* can contribute:
|
|
9
|
+
|
|
10
|
+
* by using alpha, beta, and prerelease versions
|
|
11
|
+
* by reporting bugs
|
|
12
|
+
* by suggesting new features
|
|
13
|
+
* by writing or editing documentation
|
|
14
|
+
* by writing specifications
|
|
15
|
+
* by writing code ( **no patch is too small** : fix typos, add comments, clean up inconsistent whitespace )
|
|
16
|
+
* by refactoring code
|
|
17
|
+
* by closing [issues][]
|
|
18
|
+
* by reviewing patches
|
|
19
|
+
|
|
20
|
+
[issues]: https://github.com/thoughtbot/factory_girl/issues
|
|
21
|
+
|
|
22
|
+
## Submitting an Issue
|
|
23
|
+
We use the [GitHub issue tracker][issues] to track bugs and features. Before
|
|
24
|
+
submitting a bug report or feature request, check to make sure it hasn't
|
|
25
|
+
already been submitted. When submitting a bug report, please include a [Gist][]
|
|
26
|
+
that includes a stack trace and any details that may be necessary to reproduce
|
|
27
|
+
the bug, including your gem version, Ruby version, and operating system.
|
|
28
|
+
Ideally, a bug report should include a pull request with failing specs.
|
|
29
|
+
|
|
30
|
+
[gist]: https://gist.github.com/
|
|
31
|
+
|
|
32
|
+
## Submitting a Pull Request
|
|
33
|
+
1. [Fork][fork] the [official repository][repo].
|
|
34
|
+
2. [Create a topic branch.][branch]
|
|
35
|
+
3. Implement your feature or bug fix.
|
|
36
|
+
4. Add, commit, and push your changes.
|
|
37
|
+
5. [Submit a pull request.][pr]
|
|
38
|
+
|
|
39
|
+
## Notes
|
|
40
|
+
* Please add tests if you changed code. Contributions without tests won't be accepted.
|
|
10
41
|
* Please don't update the Gem version.
|
|
42
|
+
|
|
43
|
+
[repo]: https://github.com/thoughtbot/factory_girl/tree/master
|
|
44
|
+
[fork]: https://help.github.com/articles/fork-a-repo/
|
|
45
|
+
[branch]: https://help.github.com/articles/creating-and-deleting-branches-within-your-repository/
|
|
46
|
+
[pr]: https://help.github.com/articles/using-pull-requests/
|
|
47
|
+
|
|
48
|
+
Inspired by https://github.com/middleman/middleman-heroku/blob/master/CONTRIBUTING.md
|
data/GETTING_STARTED.md
CHANGED
|
@@ -35,12 +35,28 @@ RSpec.configure do |config|
|
|
|
35
35
|
config.include FactoryGirl::Syntax::Methods
|
|
36
36
|
end
|
|
37
37
|
|
|
38
|
+
# RSpec without Rails
|
|
39
|
+
RSpec.configure do |config|
|
|
40
|
+
config.include FactoryGirl::Syntax::Methods
|
|
41
|
+
|
|
42
|
+
config.before(:suite) do
|
|
43
|
+
FactoryGirl.find_definitions
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
Remember to require the above file in your spec_helper since the support folder isn't eagerly loaded
|
|
48
|
+
|
|
49
|
+
```ruby
|
|
50
|
+
require 'support/factory_girl'
|
|
51
|
+
```
|
|
52
|
+
|
|
38
53
|
# Test::Unit
|
|
39
54
|
class Test::Unit::TestCase
|
|
40
55
|
include FactoryGirl::Syntax::Methods
|
|
41
56
|
end
|
|
42
57
|
|
|
43
58
|
# Cucumber
|
|
59
|
+
# env.rb (Rails example location - RAILS_ROOT/features/support/env.rb)
|
|
44
60
|
World(FactoryGirl::Syntax::Methods)
|
|
45
61
|
|
|
46
62
|
# Spinach
|
|
@@ -48,74 +64,24 @@ class Spinach::FeatureSteps
|
|
|
48
64
|
include FactoryGirl::Syntax::Methods
|
|
49
65
|
end
|
|
50
66
|
|
|
51
|
-
#
|
|
52
|
-
class
|
|
67
|
+
# Minitest
|
|
68
|
+
class Minitest::Unit::TestCase
|
|
53
69
|
include FactoryGirl::Syntax::Methods
|
|
54
70
|
end
|
|
55
71
|
|
|
56
|
-
#
|
|
57
|
-
class
|
|
72
|
+
# Minitest::Spec
|
|
73
|
+
class Minitest::Spec
|
|
58
74
|
include FactoryGirl::Syntax::Methods
|
|
59
75
|
end
|
|
60
76
|
|
|
61
77
|
# minitest-rails
|
|
62
|
-
class
|
|
78
|
+
class ActiveSupport::TestCase
|
|
63
79
|
include FactoryGirl::Syntax::Methods
|
|
64
80
|
end
|
|
65
81
|
```
|
|
66
82
|
|
|
67
83
|
If you do not include `FactoryGirl::Syntax::Methods` in your test suite, then all factory_girl methods will need to be prefaced with `FactoryGirl`.
|
|
68
84
|
|
|
69
|
-
Linting Factories
|
|
70
|
-
-----------------
|
|
71
|
-
|
|
72
|
-
factory_girl allows for linting known factories:
|
|
73
|
-
|
|
74
|
-
```ruby
|
|
75
|
-
FactoryGirl.lint
|
|
76
|
-
```
|
|
77
|
-
|
|
78
|
-
`FactoryGirl.lint` builds each factory and subsequently calls `#valid?` on it
|
|
79
|
-
(if `#valid?` is defined); if any calls to `#valid?` return `false`,
|
|
80
|
-
`FactoryGirl::InvalidFactoryError` is raised with a list of the offending
|
|
81
|
-
factories. Recommended usage of `FactoryGirl.lint` is to invoke this once
|
|
82
|
-
before the test suite is run.
|
|
83
|
-
|
|
84
|
-
With RSpec:
|
|
85
|
-
|
|
86
|
-
```ruby
|
|
87
|
-
# spec/support/factory_girl.rb
|
|
88
|
-
RSpec.configure do |config|
|
|
89
|
-
# additional factory_girl configuration
|
|
90
|
-
|
|
91
|
-
config.before(:suite) do
|
|
92
|
-
begin
|
|
93
|
-
DatabaseCleaner.start
|
|
94
|
-
FactoryGirl.lint
|
|
95
|
-
ensure
|
|
96
|
-
DatabaseCleaner.clean
|
|
97
|
-
end
|
|
98
|
-
end
|
|
99
|
-
end
|
|
100
|
-
```
|
|
101
|
-
|
|
102
|
-
After calling `FactoryGirl.lint`, you'll likely want to clear out the
|
|
103
|
-
database, as built factories will create associated records. The provided
|
|
104
|
-
example above uses the database_cleaner gem to clear out the database; be sure
|
|
105
|
-
to add the gem to your Gemfile under the appropriate groups.
|
|
106
|
-
|
|
107
|
-
You can lint factories selectively by passing only factories you want linted:
|
|
108
|
-
|
|
109
|
-
```ruby
|
|
110
|
-
factories_to_lint = FactoryGirl.factories.reject do |factory|
|
|
111
|
-
factory.name =~ /^old_/
|
|
112
|
-
end
|
|
113
|
-
|
|
114
|
-
FactoryGirl.lint factories_to_lint
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
This would lint all factories that aren't prefixed with `old_`.
|
|
118
|
-
|
|
119
85
|
Defining factories
|
|
120
86
|
------------------
|
|
121
87
|
|
|
@@ -143,8 +109,9 @@ It is highly recommended that you have one factory for each class that provides
|
|
|
143
109
|
|
|
144
110
|
Attempting to define multiple factories with the same name will raise an error.
|
|
145
111
|
|
|
146
|
-
Factories can be defined anywhere, but will be automatically loaded
|
|
147
|
-
are defined in files at the
|
|
112
|
+
Factories can be defined anywhere, but will be automatically loaded after
|
|
113
|
+
calling `FactoryGirl.find_definitions` if factories are defined in files at the
|
|
114
|
+
following locations:
|
|
148
115
|
|
|
149
116
|
test/factories.rb
|
|
150
117
|
spec/factories.rb
|
|
@@ -201,10 +168,18 @@ factory :user do
|
|
|
201
168
|
end
|
|
202
169
|
```
|
|
203
170
|
|
|
171
|
+
Because of the block syntax in Ruby, defining attributes as `Hash`es (for
|
|
172
|
+
serialized/JSON columns, for example) requires two sets of curly brackets:
|
|
173
|
+
|
|
174
|
+
```ruby
|
|
175
|
+
factory :program do
|
|
176
|
+
configuration { { auto_resolve: false, auto_define: true } }
|
|
177
|
+
end
|
|
178
|
+
```
|
|
179
|
+
|
|
204
180
|
Aliases
|
|
205
181
|
-------
|
|
206
|
-
|
|
207
|
-
Aliases allow you to use named associations more easily.
|
|
182
|
+
FactoryGirl allows you to define aliases to existing factories to make them easier to re-use. This could come in handy when, for example, your Post object has a author attribute that actually refers to an instance of a User class. While normally FactoryGirl can infer the factory name from the association name, in this case it will look for a author factory in vain. So, alias your user factory so it can be used under alias names.
|
|
208
183
|
|
|
209
184
|
```ruby
|
|
210
185
|
factory :user, aliases: [:author, :commenter] do
|
|
@@ -278,6 +253,22 @@ you would expect. If you need to access the evaluator in a factory_girl callback
|
|
|
278
253
|
you'll need to declare a second block argument (for the evaluator) and access
|
|
279
254
|
transient attributes from there.
|
|
280
255
|
|
|
256
|
+
Method Name / Reserved Word Attributes
|
|
257
|
+
-------------------------------
|
|
258
|
+
|
|
259
|
+
If your attributes conflict with existing methods or reserved words you can define them with `add_attribute`.
|
|
260
|
+
|
|
261
|
+
```ruby
|
|
262
|
+
factory :dna do
|
|
263
|
+
add_attribute(:sequence) { 'GATTACA' }
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
factory :payment do
|
|
267
|
+
add_attribute(:method) { 'paypal' }
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
```
|
|
271
|
+
|
|
281
272
|
Associations
|
|
282
273
|
------------
|
|
283
274
|
|
|
@@ -381,6 +372,57 @@ create(:user_with_posts).posts.length # 5
|
|
|
381
372
|
create(:user_with_posts, posts_count: 15).posts.length # 15
|
|
382
373
|
```
|
|
383
374
|
|
|
375
|
+
Generating data for a `has_and_belongs_to_many` relationship is very similar
|
|
376
|
+
to the above `has_many` relationship, with a small change, you need to pass an
|
|
377
|
+
array of objects to the model's pluralized attribute name rather than a single
|
|
378
|
+
object to the singular version of the attribute name.
|
|
379
|
+
|
|
380
|
+
Here's an example with two models that are related via
|
|
381
|
+
`has_and_belongs_to_many`:
|
|
382
|
+
|
|
383
|
+
```ruby
|
|
384
|
+
FactoryGirl.define do
|
|
385
|
+
|
|
386
|
+
# language factory with a `belongs_to` association for the profile
|
|
387
|
+
factory :language do
|
|
388
|
+
title "Through the Looking Glass"
|
|
389
|
+
profile
|
|
390
|
+
end
|
|
391
|
+
|
|
392
|
+
# profile factory without associated languages
|
|
393
|
+
factory :profile do
|
|
394
|
+
name "John Doe"
|
|
395
|
+
|
|
396
|
+
# profile_with_languages will create language data after the profile has
|
|
397
|
+
# been created
|
|
398
|
+
factory :profile_with_languages do
|
|
399
|
+
# languages_count is declared as an ignored attribute and available in
|
|
400
|
+
# attributes on the factory, as well as the callback via the evaluator
|
|
401
|
+
transient do
|
|
402
|
+
languages_count 5
|
|
403
|
+
end
|
|
404
|
+
|
|
405
|
+
# the after(:create) yields two values; the profile instance itself and
|
|
406
|
+
# the evaluator, which stores all values from the factory, including
|
|
407
|
+
# ignored attributes; `create_list`'s second argument is the number of
|
|
408
|
+
# records to create and we make sure the profile is associated properly
|
|
409
|
+
# to the language
|
|
410
|
+
after(:create) do |profile, evaluator|
|
|
411
|
+
create_list(:language, evaluator.languages_count, profiles: [profile])
|
|
412
|
+
end
|
|
413
|
+
end
|
|
414
|
+
end
|
|
415
|
+
end
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
This allows us to do:
|
|
419
|
+
|
|
420
|
+
```ruby
|
|
421
|
+
create(:profile).languages.length # 0
|
|
422
|
+
create(:profile_with_languages).languages.length # 5
|
|
423
|
+
create(:profile_with_languages, languages_count: 15).languages.length # 15
|
|
424
|
+
```
|
|
425
|
+
|
|
384
426
|
Inheritance
|
|
385
427
|
-----------
|
|
386
428
|
|
|
@@ -866,6 +908,80 @@ built_users = build_pair(:user) # array of two built users
|
|
|
866
908
|
created_users = create_pair(:user) # array of two created users
|
|
867
909
|
```
|
|
868
910
|
|
|
911
|
+
Linting Factories
|
|
912
|
+
-----------------
|
|
913
|
+
|
|
914
|
+
factory_girl allows for linting known factories:
|
|
915
|
+
|
|
916
|
+
```ruby
|
|
917
|
+
FactoryGirl.lint
|
|
918
|
+
```
|
|
919
|
+
|
|
920
|
+
`FactoryGirl.lint` creates each factory and catches any exceptions raised
|
|
921
|
+
during the creation process. `FactoryGirl::InvalidFactoryError` is raised with
|
|
922
|
+
a list of factories (and corresponding exceptions) for factories which could
|
|
923
|
+
not be created.
|
|
924
|
+
|
|
925
|
+
Recommended usage of `FactoryGirl.lint`
|
|
926
|
+
is to run this in a task
|
|
927
|
+
before your test suite is executed.
|
|
928
|
+
Running it in a `before(:suite)`,
|
|
929
|
+
will negatively impact the performance
|
|
930
|
+
of your tests
|
|
931
|
+
when running single tests.
|
|
932
|
+
|
|
933
|
+
Example Rake task:
|
|
934
|
+
|
|
935
|
+
```ruby
|
|
936
|
+
# lib/tasks/factory_girl.rake
|
|
937
|
+
namespace :factory_girl do
|
|
938
|
+
desc "Verify that all FactoryGirl factories are valid"
|
|
939
|
+
task lint: :environment do
|
|
940
|
+
if Rails.env.test?
|
|
941
|
+
begin
|
|
942
|
+
DatabaseCleaner.start
|
|
943
|
+
FactoryGirl.lint
|
|
944
|
+
ensure
|
|
945
|
+
DatabaseCleaner.clean
|
|
946
|
+
end
|
|
947
|
+
else
|
|
948
|
+
system("bundle exec rake factory_girl:lint RAILS_ENV='test'")
|
|
949
|
+
end
|
|
950
|
+
end
|
|
951
|
+
end
|
|
952
|
+
```
|
|
953
|
+
|
|
954
|
+
After calling `FactoryGirl.lint`, you'll likely want to clear out the
|
|
955
|
+
database, as records will most likely be created. The provided example above
|
|
956
|
+
uses the database_cleaner gem to clear out the database; be sure to add the
|
|
957
|
+
gem to your Gemfile under the appropriate groups.
|
|
958
|
+
|
|
959
|
+
You can lint factories selectively by passing only factories you want linted:
|
|
960
|
+
|
|
961
|
+
```ruby
|
|
962
|
+
factories_to_lint = FactoryGirl.factories.reject do |factory|
|
|
963
|
+
factory.name =~ /^old_/
|
|
964
|
+
end
|
|
965
|
+
|
|
966
|
+
FactoryGirl.lint factories_to_lint
|
|
967
|
+
```
|
|
968
|
+
|
|
969
|
+
This would lint all factories that aren't prefixed with `old_`.
|
|
970
|
+
|
|
971
|
+
Traits can also be linted. This option verifies that each
|
|
972
|
+
and every trait of a factory generates a valid object on its own.
|
|
973
|
+
This is turned on by passing `traits: true` to the `lint` method:
|
|
974
|
+
|
|
975
|
+
```ruby
|
|
976
|
+
FactoryGirl.lint traits:true
|
|
977
|
+
```
|
|
978
|
+
|
|
979
|
+
This can also be combined with other arguments:
|
|
980
|
+
|
|
981
|
+
```ruby
|
|
982
|
+
FactoryGirl.lint factories_to_lint, traits: true
|
|
983
|
+
```
|
|
984
|
+
|
|
869
985
|
Custom Construction
|
|
870
986
|
-------------------
|
|
871
987
|
|