factory_girl 3.0.0 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/GETTING_STARTED.md CHANGED
@@ -7,7 +7,7 @@ Update Your Gemfile
7
7
  If you're using Rails, you'll need to change the required version of `factory_girl_rails`:
8
8
 
9
9
  ```ruby
10
- gem "factory_girl_rails", "~> 2.0"
10
+ gem "factory_girl_rails", "~> 3.0"
11
11
  ```
12
12
 
13
13
  If you're *not* using Rails, you'll just have to change the required version of `factory_girl`:
@@ -27,23 +27,16 @@ Each factory has a name and a set of attributes. The name is used to guess the c
27
27
  # This will guess the User class
28
28
  FactoryGirl.define do
29
29
  factory :user do
30
- first_name 'John'
31
- last_name 'Doe'
30
+ first_name "John"
31
+ last_name "Doe"
32
32
  admin false
33
33
  end
34
34
 
35
35
  # This will use the User class (Admin would have been guessed)
36
- factory :admin, :class => User do
37
- first_name 'Admin'
38
- last_name 'User'
39
- admin true
40
- end
41
-
42
- # The same, but using a string instead of class constant
43
- factory :admin, :class => 'user' do
44
- first_name 'Admin'
45
- last_name 'User'
46
- admin true
36
+ factory :admin, class: User do
37
+ first_name "Admin"
38
+ last_name "User"
39
+ admin true
47
40
  end
48
41
  end
49
42
  ```
@@ -88,7 +81,7 @@ No matter which strategy is used, it's possible to override the defined attribut
88
81
 
89
82
  ```ruby
90
83
  # Build a User instance and override the first_name property
91
- user = FactoryGirl.build(:user, :first_name => 'Joe')
84
+ user = FactoryGirl.build(:user, first_name: "Joe")
92
85
  user.first_name
93
86
  # => "Joe"
94
87
  ```
@@ -103,15 +96,18 @@ end
103
96
 
104
97
  # Test::Unit
105
98
  class Test::Unit::TestCase
106
- include Factory::Syntax::Methods
99
+ include FactoryGirl::Syntax::Methods
107
100
  end
101
+
102
+ # Cucumber
103
+ World(FactoryGirl::Syntax::Methods)
108
104
  ```
109
105
 
110
106
  This would allow you to write:
111
107
 
112
108
  ```ruby
113
109
  describe User, "#full_name" do
114
- subject { create(:user, :first_name => "John", :last_name => "Doe") }
110
+ subject { create(:user, first_name: "John", last_name: "Doe") }
115
111
 
116
112
  its(:full_name) { should == "John Doe" }
117
113
  end
@@ -140,7 +136,7 @@ Aliases
140
136
  Aliases allow you to use named associations more easily.
141
137
 
142
138
  ```ruby
143
- factory :user, :aliases => [:author, :commenter] do
139
+ factory :user, aliases: [:author, :commenter] do
144
140
  first_name "John"
145
141
  last_name "Doe"
146
142
  date_of_birth { 18.years.ago }
@@ -149,7 +145,7 @@ end
149
145
  factory :post do
150
146
  author
151
147
  # instead of
152
- # association :author, :factory => :user
148
+ # association :author, factory: :user
153
149
  title "How to read a book effectively"
154
150
  body "There are five steps involved."
155
151
  end
@@ -157,7 +153,7 @@ end
157
153
  factory :comment do
158
154
  commenter
159
155
  # instead of
160
- # association :commenter, :factory => :user
156
+ # association :commenter, factory: :user
161
157
  body "Great article!"
162
158
  end
163
159
  ```
@@ -169,12 +165,12 @@ Attributes can be based on the values of other attributes using the evaluator th
169
165
 
170
166
  ```ruby
171
167
  factory :user do
172
- first_name 'Joe'
173
- last_name 'Blow'
168
+ first_name "Joe"
169
+ last_name "Blow"
174
170
  email { "#{first_name}.#{last_name}@example.com".downcase }
175
171
  end
176
172
 
177
- FactoryGirl.create(:user, :last_name => 'Doe').email
173
+ FactoryGirl.create(:user, last_name: "Doe").email
178
174
  # => "joe.doe@example.com"
179
175
  ```
180
176
 
@@ -187,7 +183,7 @@ There may be times where your code can be DRYed up by passing in transient attri
187
183
  factory :user do
188
184
  ignore do
189
185
  rockstar true
190
- upcased { false }
186
+ upcased false
191
187
  end
192
188
 
193
189
  name { "John Doe#{" - Rockstar" if rockstar}" }
@@ -198,7 +194,7 @@ factory :user do
198
194
  end
199
195
  end
200
196
 
201
- FactoryGirl.create(:user, :upcased => true).name
197
+ FactoryGirl.create(:user, upcased: true).name
202
198
  #=> "JOHN DOE - ROCKSTAR"
203
199
  ```
204
200
 
@@ -228,7 +224,7 @@ You can also specify a different factory or override attributes:
228
224
  ```ruby
229
225
  factory :post do
230
226
  # ...
231
- association :author, :factory => :user, :last_name => 'Writely'
227
+ association :author, factory: :user, last_name: "Writely"
232
228
  end
233
229
  ```
234
230
 
@@ -246,12 +242,12 @@ post.new_record? # => true
246
242
  post.author.new_record? # => false
247
243
  ```
248
244
 
249
- To not save the associated object, specify :strategy => :build in the factory:
245
+ To not save the associated object, specify strategy: :build in the factory:
250
246
 
251
247
  ```ruby
252
248
  factory :post do
253
249
  # ...
254
- association :author, :factory => :user, :strategy => :build
250
+ association :author, factory: :user, strategy: :build
255
251
  end
256
252
 
257
253
  # Builds a User, and then builds a Post, but does not save either
@@ -290,7 +286,7 @@ FactoryGirl.define do
290
286
  # attributes; `create_list`'s second argument is the number of records
291
287
  # to create and we make sure the user is associated properly to the post
292
288
  after_create do |user, evaluator|
293
- FactoryGirl.create_list(:post, evaluator.posts_count, :user => user)
289
+ FactoryGirl.create_list(:post, evaluator.posts_count, user: user)
294
290
  end
295
291
  end
296
292
  end
@@ -302,7 +298,7 @@ This allows us to do:
302
298
  ```ruby
303
299
  FactoryGirl.create(:user).posts.length # 0
304
300
  FactoryGirl.create(:user_with_posts).posts.length # 5
305
- FactoryGirl.create(:user_with_posts, :posts_count => 15).posts.length # 15
301
+ FactoryGirl.create(:user_with_posts, posts_count: 15).posts.length # 15
306
302
  ```
307
303
 
308
304
  Inheritance
@@ -312,7 +308,7 @@ You can easily create multiple factories for the same class without repeating co
312
308
 
313
309
  ```ruby
314
310
  factory :post do
315
- title 'A title'
311
+ title "A title"
316
312
 
317
313
  factory :approved_post do
318
314
  approved true
@@ -320,7 +316,7 @@ factory :post do
320
316
  end
321
317
 
322
318
  approved_post = FactoryGirl.create(:approved_post)
323
- approved_post.title # => 'A title'
319
+ approved_post.title # => "A title"
324
320
  approved_post.approved # => true
325
321
  ```
326
322
 
@@ -328,10 +324,10 @@ You can also assign the parent explicitly:
328
324
 
329
325
  ```ruby
330
326
  factory :post do
331
- title 'A title'
327
+ title "A title"
332
328
  end
333
329
 
334
- factory :approved_post, :parent => :post do
330
+ factory :approved_post, parent: :post do
335
331
  approved true
336
332
  end
337
333
  ```
@@ -405,6 +401,35 @@ factory :post do
405
401
  end
406
402
  ```
407
403
 
404
+ Sequences can also have aliases. The sequence aliases share the same counter:
405
+
406
+ ```ruby
407
+ factory :user do
408
+ sequence(:email, 1000, aliases: [:sender, :receiver]) {|n| "person#{n}@example.com" }
409
+ end
410
+
411
+ # will increase value counter for :email which is shared by :sender and :receiver
412
+ FactoryGirl.next(:sender)
413
+ ```
414
+
415
+ Define aliases and use default value (1) for the counter
416
+
417
+ ```ruby
418
+ factory :user do
419
+ sequence(:email, aliases: [:sender, :receiver]) {|n| "person#{n}@example.com" }
420
+ end
421
+ ```
422
+
423
+ Setting the value:
424
+
425
+ ```ruby
426
+ factory :user do
427
+ sequence(:email, 'a', aliases: [:sender, :receiver]) {|n| "person#{n}@example.com" }
428
+ end
429
+ ```
430
+
431
+ The value just needs to support the `#next` method. Here the next value will be 'a', then 'b', etc.
432
+
408
433
  Traits
409
434
  ------
410
435
 
@@ -412,7 +437,7 @@ Traits allow you to group attributes together and then apply them
412
437
  to any factory.
413
438
 
414
439
  ```ruby
415
- factory :user, :aliases => [:author]
440
+ factory :user, aliases: [:author]
416
441
 
417
442
  factory :story do
418
443
  title "My awesome story"
@@ -436,17 +461,17 @@ factory :story do
436
461
  end_at { Time.now }
437
462
  end
438
463
 
439
- factory :week_long_published_story, :traits => [:published, :week_long_publishing]
440
- factory :month_long_published_story, :traits => [:published, :month_long_publishing]
441
- factory :week_long_unpublished_story, :traits => [:unpublished, :week_long_publishing]
442
- factory :month_long_unpublished_story, :traits => [:unpublished, :month_long_publishing]
464
+ factory :week_long_published_story, traits: [:published, :week_long_publishing]
465
+ factory :month_long_published_story, traits: [:published, :month_long_publishing]
466
+ factory :week_long_unpublished_story, traits: [:unpublished, :week_long_publishing]
467
+ factory :month_long_unpublished_story, traits: [:unpublished, :month_long_publishing]
443
468
  end
444
469
  ```
445
470
 
446
471
  Traits can be used as attributes:
447
472
 
448
473
  ```ruby
449
- factory :week_long_published_story_with_title, :parent => :story do
474
+ factory :week_long_published_story_with_title, parent: :story do
450
475
  published
451
476
  week_long_publishing
452
477
  title { "Publishing that was started at {start_at}" }
@@ -478,8 +503,8 @@ factory :user do
478
503
  login { "admin-#{name}" }
479
504
  end
480
505
 
481
- factory :male_admin, :traits => [:male, :admin] # login will be "admin-John Doe"
482
- factory :female_admin, :traits => [:admin, :female] # login will be "Jane Doe (F)"
506
+ factory :male_admin, traits: [:male, :admin] # login will be "admin-John Doe"
507
+ factory :female_admin, traits: [:admin, :female] # login will be "Jane Doe (F)"
483
508
  end
484
509
  ```
485
510
 
@@ -520,7 +545,7 @@ factory :user do
520
545
  end
521
546
 
522
547
  # creates an admin user with gender "Male" and name "Jon Snow"
523
- FactoryGirl.create(:user, :admin, :male, :name => "Jon Snow")
548
+ FactoryGirl.create(:user, :admin, :male, name: "Jon Snow")
524
549
  ```
525
550
 
526
551
  This ability works with `build`, `build_stubbed`, `attributes_for`, and `create`.
@@ -539,7 +564,7 @@ factory :user do
539
564
  end
540
565
 
541
566
  # creates 3 admin users with gender "Male" and name "Jon Snow"
542
- FactoryGirl.create_list(:user, 3, :admin, :male, :name => "Jon Snow")
567
+ FactoryGirl.create_list(:user, 3, :admin, :male, name: "Jon Snow")
543
568
  ```
544
569
 
545
570
 
@@ -607,7 +632,7 @@ Instead of creating a child factory that added additional attributes:
607
632
 
608
633
  ```ruby
609
634
  FactoryGirl.define do
610
- factory :application_user, :parent => :user do
635
+ factory :application_user, parent: :user do
611
636
  full_name { Faker::Name.name }
612
637
  date_of_birth { 21.years.ago }
613
638
  gender "Female"
@@ -650,7 +675,7 @@ These methods will build or create a specific amount of factories and return the
650
675
  To set the attributes for each of the factories, you can pass in a hash as you normally would.
651
676
 
652
677
  ```ruby
653
- twenty_year_olds = FactoryGirl.build_list(:user, 25, :date_of_birth => 20.years.ago)
678
+ twenty_year_olds = FactoryGirl.build_list(:user, 25, date_of_birth: 20.years.ago)
654
679
  ```
655
680
 
656
681
  Custom Construction
@@ -708,7 +733,7 @@ Cucumber Integration
708
733
  factory\_girl ships with step definitions that make calling factories from Cucumber easier. To use them, add the following to features/support/env.rb:
709
734
 
710
735
  ```ruby
711
- require 'factory_girl/step_definitions'
736
+ require "factory_girl/step_definitions"
712
737
  ```
713
738
 
714
739
  Alternate Syntaxes
@@ -720,16 +745,16 @@ provide alternate interfaces. See Factory::Syntax for information about the
720
745
  various layers available. For example, the Machinist-style syntax is popular:
721
746
 
722
747
  ```ruby
723
- require 'factory_girl/syntax/blueprint'
724
- require 'factory_girl/syntax/make'
725
- require 'factory_girl/syntax/sham'
748
+ require "factory_girl/syntax/blueprint"
749
+ require "factory_girl/syntax/make"
750
+ require "factory_girl/syntax/sham"
726
751
 
727
752
  Sham.email {|n| "#{n}@example.com" }
728
753
 
729
754
  User.blueprint do
730
- name { 'Billy Bob' }
755
+ name { "Billy Bob" }
731
756
  email { Sham.email }
732
757
  end
733
758
 
734
- User.make(:name => 'Johnny')
759
+ User.make(name: "Johnny")
735
760
  ```
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- factory_girl (3.0.0)
4
+ factory_girl (3.1.0)
5
5
  activesupport (>= 3.0.0)
6
6
 
7
7
  GEM
@@ -19,7 +19,7 @@ GEM
19
19
  tzinfo (~> 0.3.29)
20
20
  activesupport (3.1.0)
21
21
  multi_json (~> 1.0)
22
- appraisal (0.3.8)
22
+ appraisal (0.4.1)
23
23
  bundler
24
24
  rake
25
25
  arel (2.2.1)
@@ -38,23 +38,23 @@ GEM
38
38
  builder (3.0.0)
39
39
  childprocess (0.2.2)
40
40
  ffi (~> 1.0.6)
41
- cucumber (1.0.6)
41
+ cucumber (1.1.9)
42
42
  builder (>= 2.1.2)
43
43
  diff-lcs (>= 1.1.2)
44
- gherkin (~> 2.4.18)
44
+ gherkin (~> 2.9.0)
45
45
  json (>= 1.4.6)
46
46
  term-ansicolor (>= 1.0.6)
47
47
  diff-lcs (1.1.3)
48
48
  ffi (1.0.9)
49
- gherkin (2.4.21)
49
+ gherkin (2.9.3)
50
50
  json (>= 1.4.6)
51
51
  i18n (0.6.0)
52
- json (1.6.1)
52
+ json (1.6.6)
53
53
  mocha (0.9.8)
54
54
  rake
55
55
  multi_json (1.0.3)
56
56
  rack (1.3.3)
57
- rake (0.9.2)
57
+ rake (0.9.2.2)
58
58
  rdiscount (1.6.8)
59
59
  rspec (2.6.0)
60
60
  rspec-core (~> 2.6.0)
@@ -71,7 +71,7 @@ GEM
71
71
  sqlite3 (1.3.4)
72
72
  sqlite3-ruby (1.3.3)
73
73
  sqlite3 (>= 1.3.3)
74
- term-ansicolor (1.0.6)
74
+ term-ansicolor (1.0.7)
75
75
  timecop (0.3.5)
76
76
  tzinfo (0.3.29)
77
77
  yard (0.7.2)
@@ -81,11 +81,11 @@ PLATFORMS
81
81
 
82
82
  DEPENDENCIES
83
83
  activerecord
84
- appraisal (~> 0.3.8)
84
+ appraisal (~> 0.4)
85
85
  aruba
86
86
  bluecloth
87
87
  bourne
88
- cucumber (~> 1.0.0)
88
+ cucumber (~> 1.1)
89
89
  factory_girl!
90
90
  mocha
91
91
  rspec (~> 2.0)
data/{Changelog → NEWS} RENAMED
@@ -1,3 +1,10 @@
1
+ 3.1.0 (April 6, 2012)
2
+ Sequences support aliases, which reference the same block
3
+ Update documentation
4
+ Add before_create callback
5
+ Support use of #attribute_names method to determine available attributes for steps
6
+ Use ActiveSupport::Deprecation for all deprecations
7
+
1
8
  3.0.0 (March 23, 2012)
2
9
  Deprecate the vintage syntax
3
10
  Remove Rails 2.x support
data/README.md CHANGED
@@ -25,6 +25,13 @@ gem 'factory_girl'
25
25
  ```
26
26
  and run `bundle install` from your shell.
27
27
 
28
+ Supported Ruby versions
29
+ -----------------------
30
+
31
+ The Factory Girl 3.x series supports Ruby 1.9.x.
32
+
33
+ For older versions of Ruby, please use the Factory Girl 2.x series.
34
+
28
35
  More Information
29
36
  ----------------
30
37
 
data/factory_girl.gemspec CHANGED
@@ -24,13 +24,13 @@ Gem::Specification.new do |s|
24
24
  s.add_dependency("activesupport", ">= 3.0.0")
25
25
 
26
26
  s.add_development_dependency("rspec", "~> 2.0")
27
- s.add_development_dependency("cucumber", "~> 1.0.0")
27
+ s.add_development_dependency("cucumber", "~> 1.1")
28
28
  s.add_development_dependency("timecop")
29
29
  s.add_development_dependency("simplecov")
30
30
  s.add_development_dependency("aruba")
31
31
  s.add_development_dependency("mocha")
32
32
  s.add_development_dependency("bourne")
33
- s.add_development_dependency("appraisal", "~> 0.3.8")
33
+ s.add_development_dependency("appraisal", "~> 0.4")
34
34
  s.add_development_dependency("sqlite3-ruby")
35
35
  s.add_development_dependency("yard")
36
36
  s.add_development_dependency("bluecloth")
@@ -235,3 +235,7 @@ Feature: Use step definitions generated by factories
235
235
  Scenario: step definitions work correctly with ORMs that have simple `columns`
236
236
  Given a simple column exists
237
237
  Then there should be 1 SimpleColumn
238
+
239
+ Scenario: step definitions work correctly with model classes that simply have attribute names
240
+ Given a named attribute model exists with a title of "a fun title"
241
+ Then there should be 1 NamedAttributeModel