factory_girl 2.0.0.beta2 → 2.0.0.beta3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. data/Appraisals +1 -0
  2. data/CONTRIBUTION_GUIDELINES.md +7 -7
  3. data/GETTING_STARTED.md +36 -11
  4. data/Gemfile +2 -2
  5. data/Gemfile.lock +28 -21
  6. data/README.md +2 -2
  7. data/Rakefile +2 -2
  8. data/features/factory_girl_steps.feature +33 -0
  9. data/features/support/factories.rb +25 -18
  10. data/features/support/test.db +0 -0
  11. data/lib/factory_girl.rb +27 -0
  12. data/lib/factory_girl/attribute.rb +4 -0
  13. data/lib/factory_girl/attribute/association.rb +4 -0
  14. data/lib/factory_girl/attribute/dynamic.rb +1 -1
  15. data/lib/factory_girl/attribute/implicit.rb +36 -0
  16. data/lib/factory_girl/attribute/sequence.rb +16 -0
  17. data/lib/factory_girl/definition_proxy.rb +2 -2
  18. data/lib/factory_girl/factory.rb +1 -13
  19. data/lib/factory_girl/find_definitions.rb +1 -3
  20. data/lib/factory_girl/proxy/build.rb +2 -2
  21. data/lib/factory_girl/proxy/stub.rb +6 -2
  22. data/lib/factory_girl/rails2.rb +7 -1
  23. data/lib/factory_girl/registry.rb +7 -21
  24. data/lib/factory_girl/sequence.rb +3 -14
  25. data/lib/factory_girl/step_definitions.rb +5 -5
  26. data/lib/factory_girl/syntax/blueprint.rb +1 -1
  27. data/lib/factory_girl/syntax/default.rb +3 -3
  28. data/lib/factory_girl/syntax/generate.rb +3 -3
  29. data/lib/factory_girl/syntax/make.rb +5 -1
  30. data/lib/factory_girl/syntax/methods.rb +51 -4
  31. data/lib/factory_girl/syntax/sham.rb +2 -2
  32. data/lib/factory_girl/syntax/vintage.rb +31 -12
  33. data/lib/factory_girl/version.rb +1 -1
  34. data/spec/acceptance/build_list_spec.rb +42 -0
  35. data/spec/acceptance/create_list_spec.rb +42 -0
  36. data/spec/acceptance/sequence_spec.rb +6 -4
  37. data/spec/acceptance/syntax/blueprint_spec.rb +3 -3
  38. data/spec/acceptance/syntax/make_spec.rb +19 -1
  39. data/spec/acceptance/syntax/vintage_spec.rb +4 -4
  40. data/spec/factory_girl/attribute/association_spec.rb +4 -0
  41. data/spec/factory_girl/attribute/implicit_spec.rb +50 -0
  42. data/spec/factory_girl/attribute/sequence_spec.rb +21 -0
  43. data/spec/factory_girl/attribute_spec.rb +4 -0
  44. data/spec/factory_girl/definition_proxy_spec.rb +2 -13
  45. data/spec/factory_girl/factory_spec.rb +4 -2
  46. data/spec/factory_girl/find_definitions_spec.rb +9 -0
  47. data/spec/factory_girl/proxy/build_spec.rb +2 -2
  48. data/spec/factory_girl/proxy/create_spec.rb +2 -2
  49. data/spec/factory_girl/proxy/stub_spec.rb +5 -1
  50. data/spec/factory_girl/registry_spec.rb +11 -20
  51. data/spec/factory_girl/sequence_spec.rb +15 -19
  52. data/spec/factory_girl_spec.rb +17 -0
  53. data/spec/spec_helper.rb +2 -1
  54. metadata +45 -73
data/Appraisals CHANGED
@@ -1,4 +1,5 @@
1
1
  appraise "2.1" do
2
+ gem "test-unit", "~> 2.3.0", :platforms => [:mri_19]
2
3
  gem "activerecord", "~> 2.1"
3
4
  end
4
5
 
@@ -1,9 +1,9 @@
1
- We're using [GitHub](http://github.com/thoughtbot/factory_girl/tree/master), and we've been getting any combination of github pull requests, patches, emails, etc. We need to normalize this workflow to make sure we don't miss any fixes.
1
+ Contributing to factory_girl:
2
2
 
3
- * Make sure you're accessing the source from the [official repository](http://github.com/thoughtbot/factory_girl/tree/master).
4
- * Please make a branch for each separate contribution. We can cherry pick your commits, but pulling from a branch is easier.
5
- * If you're submitting patches, please cut each fix or feature into a separate patch.
6
- * There should be an [issue](http://github.com/thoughtbot/factory_girl/issues) for any submission. If you've found a bug and want to fix it, open a new ticket at the same time.
7
- * Please <b>don't send pull requests</b> Just update the issue with the url for your fix when it's ready. The github pull requests pretty much get dropped on the floor until someone with commit rights notices them in the mailbox.
3
+ 1. Fork the [official repository](http://github.com/thoughtbot/factory_girl/tree/master).
4
+ 2. Make your changes in a topic branch.
5
+ 3. Send a pull request.
6
+
7
+ Notes:
8
8
  * Contributions without tests won't be accepted.
9
- * Please don't submit patches or branches that update the Gem version.
9
+ * Please don't update the Gem version.
@@ -13,14 +13,14 @@ Each factory has a name and a set of attributes. The name is used to guess the c
13
13
  last_name 'Doe'
14
14
  admin false
15
15
  end
16
-
16
+
17
17
  # This will use the User class (Admin would have been guessed)
18
18
  factory :admin, :class => User do
19
19
  first_name 'Admin'
20
20
  last_name 'User'
21
21
  admin true
22
22
  end
23
-
23
+
24
24
  # The same, but using a string instead of class constant
25
25
  factory :admin, :class => 'user' do
26
26
  first_name 'Admin'
@@ -48,13 +48,13 @@ factory_girl supports several different build strategies: build, create, attribu
48
48
 
49
49
  # Returns a User instance that's not saved
50
50
  user = FactoryGirl.build(:user)
51
-
51
+
52
52
  # Returns a saved User instance
53
53
  user = FactoryGirl.create(:user)
54
-
54
+
55
55
  # Returns a hash of attributes that can be used to build a User instance:
56
56
  attrs = FactoryGirl.attributes_for(:user)
57
-
57
+
58
58
  # Returns an object with all defined attributes stubbed out:
59
59
  stub = FactoryGirl.stub(:user)
60
60
 
@@ -143,7 +143,7 @@ You can easily create multiple factories for the same class without repeating co
143
143
  # the 'title' attribute is required for all posts
144
144
  title 'A title'
145
145
  end
146
-
146
+
147
147
  factory :approved_post, :parent => :post do
148
148
  approved true
149
149
  # the 'approver' association is required for an approved post
@@ -164,10 +164,10 @@ Factory.next:
164
164
  "person#{n}@example.com"
165
165
  end
166
166
  end
167
-
167
+
168
168
  Factory.next :email
169
169
  # => "person1@example.com"
170
-
170
+
171
171
  Factory.next :email
172
172
  # => "person2@example.com"
173
173
 
@@ -190,6 +190,18 @@ a particular factory:
190
190
  sequence(:email) {|n| "person#{n}@example.com" }
191
191
  end
192
192
 
193
+ You can also override the initial value:
194
+
195
+ factory :user do
196
+ sequence(:email, 1000) {|n| "person#{n}@example.com" }
197
+ end
198
+
199
+ Without a block, the value will increment itself, starting at its initial value:
200
+
201
+ factory :post do
202
+ sequence(:position)
203
+ end
204
+
193
205
  Callbacks
194
206
  ---------
195
207
 
@@ -226,6 +238,19 @@ Calling FactoryGirl.create will invoke both after_build and after_create callbac
226
238
 
227
239
  Also, like standard attributes, child factories will inherit (and can also define) callbacks from their parent factory.
228
240
 
241
+ Building or Creating Multiple Records
242
+ -------------------------------------
243
+
244
+ Sometimes, you'll want to create or build multiple instances of a factory at once.
245
+
246
+ built_users = FactoryGirl.build_list(:user, 25)
247
+ created_users = FactoryGirl.create_list(:user, 25)
248
+
249
+ These methods will build or create a specific amount of factories and return them as an array.
250
+ To set the attributes for each of the factories, you can pass in a hash as you normally would.
251
+
252
+ twenty_year_olds = FactoryGirl.build_list(:user, 25, :date_of_birth => 20.years.ago)
253
+
229
254
  Alternate Syntaxes
230
255
  ------------------
231
256
 
@@ -234,13 +259,13 @@ Users' tastes for syntax vary dramatically, but most users are looking for a com
234
259
  require 'factory_girl/syntax/blueprint'
235
260
  require 'factory_girl/syntax/make'
236
261
  require 'factory_girl/syntax/sham'
237
-
262
+
238
263
  Sham.email {|n| "#{n}@example.com" }
239
-
264
+
240
265
  User.blueprint do
241
266
  name { 'Billy Bob' }
242
267
  email { Sham.email }
243
268
  end
244
-
269
+
245
270
  User.make(:name => 'Johnny')
246
271
 
data/Gemfile CHANGED
@@ -1,12 +1,12 @@
1
1
  source "http://rubygems.org"
2
- gem "cucumber"
2
+ gem "cucumber", "~> 1.0.0"
3
3
  gem "rake"
4
4
  gem "rspec", "~> 2.0"
5
5
  gem "rcov"
6
6
  gem "activerecord", :require => false
7
7
  gem "rr"
8
8
  gem "sqlite3-ruby", :require => false
9
- gem "appraisal"
9
+ gem "appraisal", "~> 0.3.5"
10
10
  gem "yard"
11
11
  gem "bluecloth"
12
12
 
@@ -11,36 +11,43 @@ GEM
11
11
  arel (~> 1.0.0)
12
12
  tzinfo (~> 0.3.23)
13
13
  activesupport (3.0.1)
14
- appraisal (0.1)
14
+ appraisal (0.3.5)
15
+ aruba (~> 0.3.6)
15
16
  bundler
16
17
  rake
17
18
  arel (1.0.1)
18
19
  activesupport (~> 3.0.0)
20
+ aruba (0.3.7)
21
+ childprocess (>= 0.1.9)
22
+ cucumber (>= 0.10.5)
23
+ rspec (>= 2.6.0)
19
24
  bluecloth (2.0.9)
20
25
  builder (2.1.2)
21
- cucumber (0.9.4)
22
- builder (~> 2.1.2)
23
- diff-lcs (~> 1.1.2)
24
- gherkin (~> 2.2.9)
25
- json (~> 1.4.6)
26
- term-ansicolor (~> 1.0.5)
26
+ childprocess (0.1.9)
27
+ ffi (~> 1.0.6)
28
+ cucumber (1.0.0)
29
+ builder (>= 2.1.2)
30
+ diff-lcs (>= 1.1.2)
31
+ gherkin (~> 2.4.1)
32
+ json (>= 1.4.6)
33
+ term-ansicolor (>= 1.0.5)
27
34
  diff-lcs (1.1.2)
28
- gherkin (2.2.9)
29
- json (~> 1.4.6)
30
- term-ansicolor (~> 1.0.5)
35
+ ffi (1.0.9)
36
+ gherkin (2.4.1)
37
+ json (>= 1.4.6)
31
38
  i18n (0.4.2)
32
- json (1.4.6)
33
- rake (0.8.7)
39
+ json (1.5.3)
40
+ rake (0.9.2)
34
41
  rcov (0.9.9)
35
42
  rr (1.0.2)
36
- rspec (2.1.0)
37
- rspec-core (~> 2.1.0)
38
- rspec-expectations (~> 2.1.0)
39
- rspec-mocks (~> 2.1.0)
40
- rspec-core (2.1.0)
41
- rspec-expectations (2.1.0)
43
+ rspec (2.6.0)
44
+ rspec-core (~> 2.6.0)
45
+ rspec-expectations (~> 2.6.0)
46
+ rspec-mocks (~> 2.6.0)
47
+ rspec-core (2.6.4)
48
+ rspec-expectations (2.6.0)
42
49
  diff-lcs (~> 1.1.2)
43
- rspec-mocks (2.1.0)
50
+ rspec-mocks (2.6.0)
44
51
  sqlite3-ruby (1.3.2)
45
52
  term-ansicolor (1.0.5)
46
53
  tzinfo (0.3.23)
@@ -51,9 +58,9 @@ PLATFORMS
51
58
 
52
59
  DEPENDENCIES
53
60
  activerecord
54
- appraisal
61
+ appraisal (~> 0.3.5)
55
62
  bluecloth
56
- cucumber
63
+ cucumber (~> 1.0.0)
57
64
  rake
58
65
  rcov
59
66
  rr
data/README.md CHANGED
@@ -11,7 +11,7 @@ Documentation
11
11
 
12
12
  You should find the documentation for your version of factory_girl on [Rubygems](http://rubygems.org/gems/factory_girl).
13
13
 
14
- See {file:GETTING_STARTED.md} for information on defining and using factories.
14
+ See [GETTING_STARTED.md](http://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md) for information on defining and using factories.
15
15
 
16
16
  Download
17
17
  --------
@@ -32,7 +32,7 @@ More Information
32
32
  Contributing
33
33
  ------------
34
34
 
35
- Please read the contribution guidelines before submitting patches or pull requests.
35
+ Please see the [contribution guidelines](http://github.com/thoughtbot/factory_girl/blob/master/CONTRIBUTION_GUIDELINES.md).
36
36
 
37
37
  Credits
38
38
  -------
data/Rakefile CHANGED
@@ -11,13 +11,13 @@ require 'yard'
11
11
 
12
12
  desc 'Default: run the specs and features.'
13
13
  task :default => 'spec:unit' do
14
- system("rake -s appraisal spec:acceptance features;")
14
+ system("bundle exec rake -s appraisal spec:acceptance features;")
15
15
  end
16
16
 
17
17
  namespace :spec do
18
18
  desc "Run unit specs"
19
19
  RSpec::Core::RakeTask.new('unit') do |t|
20
- t.pattern = 'spec/factory_girl/**/*_spec.rb'
20
+ t.pattern = 'spec/{*_spec.rb,factory_girl/**/*_spec.rb}'
21
21
  end
22
22
 
23
23
  desc "Run acceptance specs"
@@ -149,3 +149,36 @@ Feature: Use step definitions generated by factories
149
149
  | category_group_id |
150
150
  | 456 |
151
151
 
152
+ Scenario: factory name and attributes should not be case sensitive
153
+ Given the following category exists:
154
+ | name | category group |
155
+ | fiction | Name: books |
156
+ Then there should be 1 category
157
+ Given the following Category exists:
158
+ | name | category group |
159
+ | science | Name: books |
160
+ Then there should be 2 categories
161
+
162
+ Scenario: factory name and attributes should not be case sensitive
163
+ Given a user exists
164
+ Then there should be 1 user
165
+ Given a User exists
166
+ Then there should be 2 Users
167
+
168
+ Scenario: factory name and attributes should not be case sensitive
169
+ Given 3 users exist
170
+ Then there should be 3 users
171
+ Given 3 Users exist
172
+ Then there should be 6 Users
173
+
174
+ Scenario: factory name and attributes should not be case sensitive
175
+ Given a category exists with a name of "fiction"
176
+ Then there should be 1 category
177
+ Given a Category exists with a name of "science"
178
+ Then there should be 2 Categories
179
+
180
+ Scenario: factory name and attributes should not be case sensitive
181
+ Given 3 categories exist with a name of "fiction"
182
+ Then there should be 3 categories
183
+ Given 3 Categories exist with a name of "science"
184
+ Then there should be 6 Categories
@@ -48,29 +48,36 @@ end
48
48
  class NonActiveRecord
49
49
  end
50
50
 
51
- Factory.define :user do |f|
52
- end
51
+ FactoryGirl.define do
52
+ # To make sure the step defs work with an email
53
+ sequence :email do |n|
54
+ "email#{n}@example.com"
55
+ end
53
56
 
54
- Factory.define :admin_user, :parent => :user do |f|
55
- f.admin true
56
- end
57
+ factory :user do
58
+ end
57
59
 
58
- Factory.define :category do |f|
59
- f.name "programming"
60
- f.association :category_group
61
- end
60
+ factory :admin_user, :parent => :user do
61
+ admin true
62
+ end
62
63
 
63
- Factory.define :category_group do |f|
64
- f.name "tecnhology"
65
- end
64
+ factory :category do
65
+ name "programming"
66
+ category_group
67
+ end
66
68
 
67
- Factory.define :post do |f|
68
- f.association :author, :factory => :user
69
- f.association :category
70
- end
69
+ factory :category_group do
70
+ name "tecnhology"
71
+ end
71
72
 
72
- # This is here to ensure that factory step definitions don't raise for a non-AR factory
73
- Factory.define :non_active_record do |f|
73
+ factory :post do
74
+ association :author, :factory => :user
75
+ category
76
+ end
77
+
78
+ # This is here to ensure that factory step definitions don't raise for a non-AR factory
79
+ factory :non_active_record do
80
+ end
74
81
  end
75
82
 
76
83
  require 'factory_girl/step_definitions'
Binary file
@@ -10,6 +10,8 @@ require 'factory_girl/attribute/static'
10
10
  require 'factory_girl/attribute/dynamic'
11
11
  require 'factory_girl/attribute/association'
12
12
  require 'factory_girl/attribute/callback'
13
+ require 'factory_girl/attribute/sequence'
14
+ require 'factory_girl/attribute/implicit'
13
15
  require 'factory_girl/sequence'
14
16
  require 'factory_girl/aliases'
15
17
  require 'factory_girl/definition_proxy'
@@ -24,3 +26,28 @@ if defined?(Rails) && Rails::VERSION::MAJOR == 2
24
26
  require 'factory_girl/rails2'
25
27
  end
26
28
 
29
+ module FactoryGirl
30
+ def self.factories
31
+ @factories ||= Registry.new
32
+ end
33
+
34
+ def self.register_factory(factory)
35
+ factories.add(factory)
36
+ end
37
+
38
+ def self.factory_by_name(name)
39
+ factories.find(name)
40
+ end
41
+
42
+ def self.sequences
43
+ @sequences ||= Registry.new
44
+ end
45
+
46
+ def self.register_sequence(sequence)
47
+ sequences.add(sequence)
48
+ end
49
+
50
+ def self.sequence_by_name(name)
51
+ sequences.find(name)
52
+ end
53
+ end
@@ -24,6 +24,10 @@ module FactoryGirl
24
24
 
25
25
  def add_to(proxy)
26
26
  end
27
+
28
+ def association?
29
+ false
30
+ end
27
31
  end
28
32
 
29
33
  end
@@ -14,6 +14,10 @@ module FactoryGirl
14
14
  def add_to(proxy)
15
15
  proxy.associate(name, @factory, @overrides)
16
16
  end
17
+
18
+ def association?
19
+ true
20
+ end
17
21
  end
18
22
 
19
23
  end
@@ -8,7 +8,7 @@ module FactoryGirl
8
8
  end
9
9
 
10
10
  def add_to(proxy)
11
- value = @block.arity == 1 ? @block.call(proxy) : proxy.instance_eval(&@block)
11
+ value = @block.arity == 1 ? @block.call(proxy) : proxy.instance_exec(&@block)
12
12
  if FactoryGirl::Sequence === value
13
13
  raise SequenceAbuseError
14
14
  end
@@ -0,0 +1,36 @@
1
+ module FactoryGirl
2
+ class Attribute
3
+
4
+ class Implicit < Attribute
5
+ def initialize(name)
6
+ super(name)
7
+ end
8
+
9
+ def add_to(proxy)
10
+ implementation.add_to(proxy)
11
+ end
12
+
13
+ def association?
14
+ implementation.association?
15
+ end
16
+
17
+ def factory
18
+ name
19
+ end
20
+
21
+ private
22
+
23
+ def implementation
24
+ @implementation ||= resolve_name
25
+ end
26
+
27
+ def resolve_name
28
+ if FactoryGirl.factories.registered?(name)
29
+ Attribute::Association.new(name, name, {})
30
+ else
31
+ Attribute::Sequence.new(name, name)
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end