factory_girl 2.0.5 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +9 -0
- data/.gitignore +8 -0
- data/.rspec +2 -0
- data/.travis.yml +10 -0
- data/.yardopts +5 -0
- data/Appraisals +3 -0
- data/GETTING_STARTED.md +67 -8
- data/Gemfile +2 -11
- data/Gemfile.lock +51 -34
- data/README.md +3 -1
- data/cucumber.yml +1 -0
- data/features/find_definitions.feature +21 -8
- data/features/step_definitions/factory_girl_steps.rb +4 -4
- data/gemfiles/2.1.gemfile +8 -0
- data/gemfiles/2.1.gemfile.lock +73 -0
- data/gemfiles/2.3.gemfile +7 -0
- data/gemfiles/2.3.gemfile.lock +71 -0
- data/gemfiles/3.0.gemfile +7 -0
- data/gemfiles/3.0.gemfile.lock +81 -0
- data/gemfiles/3.1.gemfile +7 -0
- data/gemfiles/3.1.gemfile.lock +91 -0
- data/lib/factory_girl.rb +1 -0
- data/lib/factory_girl/attribute.rb +4 -0
- data/lib/factory_girl/attribute_list.rb +35 -13
- data/lib/factory_girl/factory.rb +36 -9
- data/lib/factory_girl/proxy/build.rb +21 -2
- data/lib/factory_girl/proxy/create.rb +6 -0
- data/lib/factory_girl/proxy/stub.rb +10 -2
- data/lib/factory_girl/rails2.rb +1 -0
- data/lib/factory_girl/reload.rb +8 -0
- data/lib/factory_girl/syntax/default.rb +16 -0
- data/lib/factory_girl/version.rb +1 -1
- data/spec/acceptance/build_spec.rb +31 -0
- data/spec/acceptance/create_spec.rb +27 -1
- data/spec/acceptance/modify_factories_spec.rb +184 -0
- data/spec/acceptance/stub_spec.rb +64 -0
- data/spec/factory_girl/attribute_list_spec.rb +54 -2
- data/spec/factory_girl/factory_spec.rb +6 -4
- data/spec/factory_girl/proxy/build_spec.rb +24 -0
- data/spec/factory_girl/proxy/stub_spec.rb +14 -0
- data/spec/spec_helper.rb +1 -0
- data/spec/support/shared_examples/proxy.rb +29 -0
- metadata +109 -57
- data/features/support/test.db +0 -0
data/.autotest
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
Autotest.add_hook :initialize do |at|
|
2
|
+
at.clear_mappings
|
3
|
+
|
4
|
+
at.add_mapping(%r{^test/.*_test\.rb$}) {|f, _| [f] }
|
5
|
+
at.add_mapping(%r{^lib/factory_girl/(.*)\.rb$}) {|_, m| ["test/#{m[1]}_test.rb",
|
6
|
+
"test/integration_test.rb"] }
|
7
|
+
at.add_mapping(%r{^test/(test_helper|models)\.rb$}) { at.files_matching %r{^test/.*_test\.rb$} }
|
8
|
+
at.add_mapping(%r{^lib/.*\.rb$}) { at.files_matching %r{^test/.*_test\.rb$} }
|
9
|
+
end
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/.yardopts
ADDED
data/Appraisals
CHANGED
data/GETTING_STARTED.md
CHANGED
@@ -69,7 +69,7 @@ factory_girl supports several different build strategies: build, create, attribu
|
|
69
69
|
attrs = FactoryGirl.attributes_for(:user)
|
70
70
|
|
71
71
|
# Returns an object with all defined attributes stubbed out
|
72
|
-
stub = FactoryGirl.
|
72
|
+
stub = FactoryGirl.build_stubbed(:user)
|
73
73
|
|
74
74
|
No matter which strategy is used, it's possible to override the defined attributes by passing a hash:
|
75
75
|
|
@@ -166,8 +166,8 @@ There may be times where your code can be DRYed up by passing in transient attri
|
|
166
166
|
end
|
167
167
|
end
|
168
168
|
|
169
|
-
FactoryGirl.create(:user, :upcased => true).name
|
170
|
-
|
169
|
+
FactoryGirl.create(:user, :upcased => true).name
|
170
|
+
#=> "JOHN DOE - ROCKSTAR"
|
171
171
|
|
172
172
|
Static and dynamic attributes can be ignored. Ignored attributes will be ignored within attributes_for and won't be set on the model, even if the attribute exists or you attempt to override it.
|
173
173
|
|
@@ -176,7 +176,7 @@ Within Factory Girl's dynamic attributes, you can access ignored attributes as y
|
|
176
176
|
Associations
|
177
177
|
------------
|
178
178
|
|
179
|
-
It's
|
179
|
+
It's possible to set up associations within factories. If the factory name is the same as the association name, the factory name can be left out.
|
180
180
|
|
181
181
|
factory :post do
|
182
182
|
# ...
|
@@ -194,13 +194,25 @@ The behavior of the association method varies depending on the build strategy us
|
|
194
194
|
|
195
195
|
# Builds and saves a User and a Post
|
196
196
|
post = FactoryGirl.create(:post)
|
197
|
-
post.new_record?
|
198
|
-
post.author.new_record # => false
|
197
|
+
post.new_record? # => false
|
198
|
+
post.author.new_record? # => false
|
199
199
|
|
200
200
|
# Builds and saves a User, and then builds but does not save a Post
|
201
201
|
post = FactoryGirl.build(:post)
|
202
|
-
post.new_record?
|
203
|
-
post.author.new_record # => false
|
202
|
+
post.new_record? # => true
|
203
|
+
post.author.new_record? # => false
|
204
|
+
|
205
|
+
To not save the associated object, specify :method => :build in the factory:
|
206
|
+
|
207
|
+
factory :post do
|
208
|
+
# ...
|
209
|
+
association :author, :factory => :user, :method => :build
|
210
|
+
end
|
211
|
+
|
212
|
+
# Builds a User, and then builds a Post, but does not save either
|
213
|
+
post = FactoryGirl.build(:post)
|
214
|
+
post.new_record? # => true
|
215
|
+
post.author.new_record? # => true
|
204
216
|
|
205
217
|
Inheritance
|
206
218
|
-----------
|
@@ -391,6 +403,53 @@ Calling FactoryGirl.create will invoke both after_build and after_create callbac
|
|
391
403
|
|
392
404
|
Also, like standard attributes, child factories will inherit (and can also define) callbacks from their parent factory.
|
393
405
|
|
406
|
+
Modifying factories
|
407
|
+
-------------------
|
408
|
+
|
409
|
+
If you're given a set of factories (say, from a gem developer) but want to change them to fit into your application better, you can
|
410
|
+
modify that factory instead of creating a child factory and adding attributes there.
|
411
|
+
|
412
|
+
If a gem were to give you a User factory:
|
413
|
+
|
414
|
+
FactoryGirl.define do
|
415
|
+
factory :user do
|
416
|
+
full_name "John Doe"
|
417
|
+
sequence(:username) {|n| "user#{n}" }
|
418
|
+
password "password"
|
419
|
+
end
|
420
|
+
end
|
421
|
+
|
422
|
+
Instead of creating a child factory that added additional attributes:
|
423
|
+
|
424
|
+
FactoryGirl.define do
|
425
|
+
factory :application_user, :parent => :user do
|
426
|
+
full_name { Faker::Name.name }
|
427
|
+
date_of_birth { 21.years.ago }
|
428
|
+
gender "Female"
|
429
|
+
health 90
|
430
|
+
end
|
431
|
+
end
|
432
|
+
|
433
|
+
You could modify that factory instead.
|
434
|
+
|
435
|
+
FactoryGirl.modify do
|
436
|
+
factory :user do
|
437
|
+
full_name { Faker::Name.name }
|
438
|
+
date_of_birth { 21.years.ago }
|
439
|
+
gender "Female"
|
440
|
+
health 90
|
441
|
+
end
|
442
|
+
end
|
443
|
+
|
444
|
+
When modifying a factory, you can change any of the attributes you want (aside from callbacks).
|
445
|
+
|
446
|
+
`FactoryGirl.modify` must be called outside of a `FactoryGirl.define` block as it operates on factories differently.
|
447
|
+
|
448
|
+
A couple caveats: you can only modify factories (not sequences or traits) and callbacks *still compound as they normally would*. So, if
|
449
|
+
the factory you're modifying defines an `after_create` callback, you defining an `after_create` won't override it, it'll just get run after the first callback.
|
450
|
+
You also can't modify attributes assigned by traits. So, if you have a trait that grants a name attribute, and you modify the factory to set the name,
|
451
|
+
it currently will reflect the name in the trait instead of the modified name.
|
452
|
+
|
394
453
|
Building or Creating Multiple Records
|
395
454
|
-------------------------------------
|
396
455
|
|
data/Gemfile
CHANGED
@@ -1,14 +1,5 @@
|
|
1
1
|
source "http://rubygems.org"
|
2
|
-
|
3
|
-
gem "rake"
|
4
|
-
gem "rspec", "~> 2.0"
|
5
|
-
gem "rcov"
|
2
|
+
|
6
3
|
gem "activerecord", :require => false
|
7
|
-
gem "activesupport", :require => false
|
8
|
-
gem "mocha"
|
9
|
-
gem "bourne"
|
10
|
-
gem "sqlite3-ruby", :require => false
|
11
|
-
gem "appraisal", "~> 0.3.5"
|
12
|
-
gem "yard"
|
13
|
-
gem "bluecloth"
|
14
4
|
|
5
|
+
gemspec
|
data/Gemfile.lock
CHANGED
@@ -1,48 +1,61 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
factory_girl (2.1.0)
|
5
|
+
|
1
6
|
GEM
|
2
7
|
remote: http://rubygems.org/
|
3
8
|
specs:
|
4
|
-
activemodel (3.0
|
5
|
-
activesupport (= 3.0
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
9
|
+
activemodel (3.1.0)
|
10
|
+
activesupport (= 3.1.0)
|
11
|
+
bcrypt-ruby (~> 3.0.0)
|
12
|
+
builder (~> 3.0.0)
|
13
|
+
i18n (~> 0.6)
|
14
|
+
activerecord (3.1.0)
|
15
|
+
activemodel (= 3.1.0)
|
16
|
+
activesupport (= 3.1.0)
|
17
|
+
arel (~> 2.2.1)
|
18
|
+
tzinfo (~> 0.3.29)
|
19
|
+
activesupport (3.1.0)
|
20
|
+
multi_json (~> 1.0)
|
21
|
+
appraisal (0.3.8)
|
16
22
|
bundler
|
17
23
|
rake
|
18
|
-
arel (
|
19
|
-
|
20
|
-
|
21
|
-
childprocess (>= 0.
|
22
|
-
cucumber (>= 0.
|
24
|
+
arel (2.2.1)
|
25
|
+
aruba (0.4.6)
|
26
|
+
bcat (>= 0.6.1)
|
27
|
+
childprocess (>= 0.2.0)
|
28
|
+
cucumber (>= 1.0.2)
|
29
|
+
rdiscount (>= 1.6.8)
|
23
30
|
rspec (>= 2.6.0)
|
24
|
-
|
31
|
+
bcat (0.6.1)
|
32
|
+
rack (~> 1.0)
|
33
|
+
bcrypt-ruby (3.0.0)
|
34
|
+
bluecloth (2.1.0)
|
25
35
|
bourne (1.0)
|
26
36
|
mocha (= 0.9.8)
|
27
|
-
builder (
|
28
|
-
childprocess (0.
|
37
|
+
builder (3.0.0)
|
38
|
+
childprocess (0.2.2)
|
29
39
|
ffi (~> 1.0.6)
|
30
|
-
cucumber (1.0.
|
40
|
+
cucumber (1.0.2)
|
31
41
|
builder (>= 2.1.2)
|
32
42
|
diff-lcs (>= 1.1.2)
|
33
|
-
gherkin (~> 2.4.
|
43
|
+
gherkin (~> 2.4.5)
|
34
44
|
json (>= 1.4.6)
|
35
45
|
term-ansicolor (>= 1.0.5)
|
36
|
-
diff-lcs (1.1.
|
46
|
+
diff-lcs (1.1.3)
|
37
47
|
ffi (1.0.9)
|
38
|
-
gherkin (2.4.
|
48
|
+
gherkin (2.4.16)
|
39
49
|
json (>= 1.4.6)
|
40
|
-
i18n (0.
|
41
|
-
json (1.5.
|
50
|
+
i18n (0.6.0)
|
51
|
+
json (1.5.4)
|
42
52
|
mocha (0.9.8)
|
43
53
|
rake
|
54
|
+
multi_json (1.0.3)
|
55
|
+
rack (1.3.2)
|
44
56
|
rake (0.9.2)
|
45
|
-
rcov (0.9.
|
57
|
+
rcov (0.9.10)
|
58
|
+
rdiscount (1.6.8)
|
46
59
|
rspec (2.6.0)
|
47
60
|
rspec-core (~> 2.6.0)
|
48
61
|
rspec-expectations (~> 2.6.0)
|
@@ -51,24 +64,28 @@ GEM
|
|
51
64
|
rspec-expectations (2.6.0)
|
52
65
|
diff-lcs (~> 1.1.2)
|
53
66
|
rspec-mocks (2.6.0)
|
54
|
-
sqlite3
|
55
|
-
|
56
|
-
|
57
|
-
|
67
|
+
sqlite3 (1.3.4)
|
68
|
+
sqlite3-ruby (1.3.3)
|
69
|
+
sqlite3 (>= 1.3.3)
|
70
|
+
term-ansicolor (1.0.6)
|
71
|
+
timecop (0.3.5)
|
72
|
+
tzinfo (0.3.29)
|
73
|
+
yard (0.7.2)
|
58
74
|
|
59
75
|
PLATFORMS
|
60
76
|
ruby
|
61
77
|
|
62
78
|
DEPENDENCIES
|
63
79
|
activerecord
|
64
|
-
|
65
|
-
|
80
|
+
appraisal (~> 0.3.8)
|
81
|
+
aruba
|
66
82
|
bluecloth
|
67
83
|
bourne
|
68
84
|
cucumber (~> 1.0.0)
|
85
|
+
factory_girl!
|
69
86
|
mocha
|
70
|
-
rake
|
71
87
|
rcov
|
72
88
|
rspec (~> 2.0)
|
73
89
|
sqlite3-ruby
|
90
|
+
timecop
|
74
91
|
yard
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# factory_girl [![Build Status](https://secure.travis-ci.org/thoughtbot/factory_girl.png)](http://travis-ci.org/thoughtbot/factory_girl)
|
1
|
+
# factory_girl [![Build Status](https://secure.travis-ci.org/thoughtbot/factory_girl.png)](http://travis-ci.org/thoughtbot/factory_girl?branch=master)
|
2
2
|
|
3
3
|
factory_girl 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
|
|
@@ -49,6 +49,8 @@ factory_girl was written by Joe Ferris with contributions from several authors,
|
|
49
49
|
* Josh Nichols
|
50
50
|
* Josh Owens
|
51
51
|
* Nate Sutton
|
52
|
+
* Josh Clayton
|
53
|
+
* Thomas Walpole
|
52
54
|
|
53
55
|
The syntax layers are derived from software written by the following authors:
|
54
56
|
|
data/cucumber.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
default: -r features features
|
@@ -44,19 +44,32 @@ Feature: Factory girl can find factory definitions correctly
|
|
44
44
|
| name |
|
45
45
|
| great!!! |
|
46
46
|
|
47
|
-
Scenario:
|
48
|
-
Given a file named "nested/
|
47
|
+
Scenario: Reload Factory Girl
|
48
|
+
Given a file named "nested/reload_factories.rb" with:
|
49
49
|
"""
|
50
50
|
FactoryGirl.define do
|
51
|
-
|
52
|
-
|
51
|
+
sequence(:great)
|
52
|
+
trait :admin do
|
53
|
+
admin true
|
54
|
+
end
|
55
|
+
|
56
|
+
factory :handy_category, :class => Category do
|
57
|
+
name "handy"
|
53
58
|
end
|
54
59
|
end
|
55
60
|
"""
|
56
61
|
When "nested" is added to Factory Girl's file definitions path
|
57
|
-
And I
|
58
|
-
|
59
|
-
|
62
|
+
And I append to "nested/reload_factories.rb" with:
|
63
|
+
"""
|
64
|
+
|
65
|
+
FactoryGirl.modify do
|
66
|
+
factory :handy_category do
|
67
|
+
name "HANDY!!!"
|
68
|
+
end
|
69
|
+
end
|
70
|
+
"""
|
71
|
+
And I reload factories
|
72
|
+
And I create a "handy_category" instance from Factory Girl
|
60
73
|
Then I should find the following for the last category:
|
61
74
|
| name |
|
62
|
-
|
|
75
|
+
| HANDY!!! |
|
@@ -34,10 +34,10 @@ Given /^these super users exist:$/ do |table|
|
|
34
34
|
Given %{the following person exists:}, new_table
|
35
35
|
end
|
36
36
|
|
37
|
-
When /^I clear out the factories$/ do
|
38
|
-
FactoryGirl.factories.clear
|
39
|
-
end
|
40
|
-
|
41
37
|
When /^I find definitions$/ do
|
42
38
|
FactoryGirl.find_definitions
|
43
39
|
end
|
40
|
+
|
41
|
+
When /^I reload factories$/ do
|
42
|
+
FactoryGirl.reload
|
43
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
PATH
|
2
|
+
remote: /Users/joshuaclayton/dev/gems/factory_girl
|
3
|
+
specs:
|
4
|
+
factory_girl (2.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
activerecord (2.3.12)
|
10
|
+
activesupport (= 2.3.12)
|
11
|
+
activesupport (2.3.12)
|
12
|
+
appraisal (0.3.8)
|
13
|
+
bundler
|
14
|
+
rake
|
15
|
+
aruba (0.3.7)
|
16
|
+
childprocess (>= 0.1.9)
|
17
|
+
cucumber (>= 0.10.5)
|
18
|
+
rspec (>= 2.6.0)
|
19
|
+
bluecloth (2.1.0)
|
20
|
+
bourne (1.0)
|
21
|
+
mocha (= 0.9.8)
|
22
|
+
builder (3.0.0)
|
23
|
+
childprocess (0.1.9)
|
24
|
+
ffi (~> 1.0.6)
|
25
|
+
cucumber (1.0.0)
|
26
|
+
builder (>= 2.1.2)
|
27
|
+
diff-lcs (>= 1.1.2)
|
28
|
+
gherkin (~> 2.4.1)
|
29
|
+
json (>= 1.4.6)
|
30
|
+
term-ansicolor (>= 1.0.5)
|
31
|
+
diff-lcs (1.1.2)
|
32
|
+
ffi (1.0.9)
|
33
|
+
gherkin (2.4.1)
|
34
|
+
json (>= 1.4.6)
|
35
|
+
json (1.5.3)
|
36
|
+
mocha (0.9.8)
|
37
|
+
rake
|
38
|
+
rake (0.9.2)
|
39
|
+
rcov (0.9.9)
|
40
|
+
rspec (2.6.0)
|
41
|
+
rspec-core (~> 2.6.0)
|
42
|
+
rspec-expectations (~> 2.6.0)
|
43
|
+
rspec-mocks (~> 2.6.0)
|
44
|
+
rspec-core (2.6.4)
|
45
|
+
rspec-expectations (2.6.0)
|
46
|
+
diff-lcs (~> 1.1.2)
|
47
|
+
rspec-mocks (2.6.0)
|
48
|
+
sqlite3 (1.3.3)
|
49
|
+
sqlite3-ruby (1.3.3)
|
50
|
+
sqlite3 (>= 1.3.3)
|
51
|
+
term-ansicolor (1.0.5)
|
52
|
+
test-unit (2.3.0)
|
53
|
+
timecop (0.3.5)
|
54
|
+
yard (0.7.2)
|
55
|
+
|
56
|
+
PLATFORMS
|
57
|
+
ruby
|
58
|
+
|
59
|
+
DEPENDENCIES
|
60
|
+
activerecord (~> 2.1)
|
61
|
+
appraisal (~> 0.3.8)
|
62
|
+
aruba
|
63
|
+
bluecloth
|
64
|
+
bourne
|
65
|
+
cucumber (~> 1.0.0)
|
66
|
+
factory_girl!
|
67
|
+
mocha
|
68
|
+
rcov
|
69
|
+
rspec (~> 2.0)
|
70
|
+
sqlite3-ruby
|
71
|
+
test-unit (~> 2.3.0)
|
72
|
+
timecop
|
73
|
+
yard
|