jeffrafter-factory_girl 1.2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. data/CONTRIBUTION_GUIDELINES.rdoc +9 -0
  2. data/Changelog +29 -0
  3. data/LICENSE +19 -0
  4. data/README.rdoc +265 -0
  5. data/Rakefile +81 -0
  6. data/lib/factory_girl.rb +35 -0
  7. data/lib/factory_girl/aliases.rb +50 -0
  8. data/lib/factory_girl/attribute.rb +29 -0
  9. data/lib/factory_girl/attribute/association.rb +20 -0
  10. data/lib/factory_girl/attribute/callback.rb +16 -0
  11. data/lib/factory_girl/attribute/dynamic.rb +20 -0
  12. data/lib/factory_girl/attribute/static.rb +17 -0
  13. data/lib/factory_girl/factory.rb +430 -0
  14. data/lib/factory_girl/proxy.rb +79 -0
  15. data/lib/factory_girl/proxy/attributes_for.rb +21 -0
  16. data/lib/factory_girl/proxy/build.rb +30 -0
  17. data/lib/factory_girl/proxy/create.rb +12 -0
  18. data/lib/factory_girl/proxy/stub.rb +50 -0
  19. data/lib/factory_girl/sequence.rb +63 -0
  20. data/lib/factory_girl/step_definitions.rb +54 -0
  21. data/lib/factory_girl/syntax.rb +12 -0
  22. data/lib/factory_girl/syntax/blueprint.rb +42 -0
  23. data/lib/factory_girl/syntax/generate.rb +68 -0
  24. data/lib/factory_girl/syntax/make.rb +39 -0
  25. data/lib/factory_girl/syntax/sham.rb +42 -0
  26. data/spec/factory_girl/aliases_spec.rb +29 -0
  27. data/spec/factory_girl/attribute/association_spec.rb +29 -0
  28. data/spec/factory_girl/attribute/callback_spec.rb +23 -0
  29. data/spec/factory_girl/attribute/dynamic_spec.rb +49 -0
  30. data/spec/factory_girl/attribute/static_spec.rb +29 -0
  31. data/spec/factory_girl/attribute_spec.rb +30 -0
  32. data/spec/factory_girl/factory_spec.rb +605 -0
  33. data/spec/factory_girl/proxy/attributes_for_spec.rb +52 -0
  34. data/spec/factory_girl/proxy/build_spec.rb +81 -0
  35. data/spec/factory_girl/proxy/create_spec.rb +94 -0
  36. data/spec/factory_girl/proxy/stub_spec.rb +79 -0
  37. data/spec/factory_girl/proxy_spec.rb +84 -0
  38. data/spec/factory_girl/sequence_spec.rb +66 -0
  39. data/spec/factory_girl/syntax/blueprint_spec.rb +34 -0
  40. data/spec/factory_girl/syntax/generate_spec.rb +57 -0
  41. data/spec/factory_girl/syntax/make_spec.rb +35 -0
  42. data/spec/factory_girl/syntax/sham_spec.rb +35 -0
  43. data/spec/integration_spec.rb +304 -0
  44. data/spec/models.rb +43 -0
  45. data/spec/spec_helper.rb +18 -0
  46. metadata +128 -0
@@ -0,0 +1,34 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ require 'factory_girl/syntax/blueprint'
4
+
5
+ describe "a blueprint" do
6
+ before do
7
+ Factory.sequence(:email) { |n| "somebody#{n}@example.com" }
8
+ User.blueprint do
9
+ first_name { 'Bill' }
10
+ last_name { 'Nye' }
11
+ email { Factory.next(:email) }
12
+ end
13
+ end
14
+
15
+ after do
16
+ Factory.factories.clear
17
+ Factory.sequences.clear
18
+ end
19
+
20
+ describe "after making an instance" do
21
+ before do
22
+ @instance = Factory(:user, :last_name => 'Rye')
23
+ end
24
+
25
+ it "should use attributes from the blueprint" do
26
+ @instance.first_name.should == 'Bill'
27
+ end
28
+
29
+ it "should evaluate attribute blocks for each instance" do
30
+ @instance.email.should =~ /somebody\d+@example.com/
31
+ Factory(:user).email.should_not == @instance.email
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,57 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
2
+
3
+ require 'factory_girl/syntax/generate'
4
+
5
+ describe "a factory using generate syntax" do
6
+ before do
7
+ Factory.define :user do |factory|
8
+ factory.first_name 'Bill'
9
+ factory.last_name 'Nye'
10
+ factory.email 'science@guys.net'
11
+ end
12
+ end
13
+
14
+ after do
15
+ Factory.factories.clear
16
+ end
17
+
18
+ it "should not raise an error when generating an invalid instance" do
19
+ lambda { User.generate(:first_name => nil) }.should_not raise_error
20
+ end
21
+
22
+ it "should raise an error when forcefully generating an invalid instance" do
23
+ lambda { User.generate!(:first_name => nil) }.should raise_error(ActiveRecord::RecordInvalid)
24
+ end
25
+
26
+ %w(generate generate! spawn).each do |method|
27
+ it "should yield a generated instance when using #{method} with a block" do
28
+ saved_instance = nil
29
+ User.send(method) {|instance| saved_instance = instance }
30
+ saved_instance.should be_kind_of(User)
31
+ end
32
+
33
+ describe "after generating an instance using #{method}" do
34
+ before do
35
+ @instance = User.send(method, :last_name => 'Rye')
36
+ end
37
+
38
+ it "should use attributes from the factory" do
39
+ @instance.first_name.should == 'Bill'
40
+ end
41
+
42
+ it "should use attributes passed to generate" do
43
+ @instance.last_name.should == 'Rye'
44
+ end
45
+
46
+ if method == 'spawn'
47
+ it "should not save the record" do
48
+ @instance.should be_new_record
49
+ end
50
+ else
51
+ it "should save the record" do
52
+ @instance.should_not be_new_record
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,35 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ require 'factory_girl/syntax/make'
4
+
5
+ describe "a factory using make syntax" do
6
+ before do
7
+ Factory.define :user do |factory|
8
+ factory.first_name 'Bill'
9
+ factory.last_name 'Nye'
10
+ factory.email 'science@guys.net'
11
+ end
12
+ end
13
+
14
+ after do
15
+ Factory.factories.clear
16
+ end
17
+
18
+ describe "after making an instance" do
19
+ before do
20
+ @instance = User.make(:last_name => 'Rye')
21
+ end
22
+
23
+ it "should use attributes from the factory" do
24
+ @instance.first_name.should == 'Bill'
25
+ end
26
+
27
+ it "should use attributes passed to make" do
28
+ @instance.last_name.should == 'Rye'
29
+ end
30
+
31
+ it "should save the record" do
32
+ @instance.should_not be_new_record
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,35 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ require 'factory_girl/syntax/sham'
4
+
5
+ describe "a factory using sham syntax" do
6
+ before do
7
+ Sham.name { "Name" }
8
+ Sham.email { "somebody#{rand(5)}@example.com" }
9
+
10
+ Factory.define :user do |factory|
11
+ factory.first_name { Sham.name }
12
+ factory.last_name { Sham.name }
13
+ factory.email { Sham.email }
14
+ end
15
+ end
16
+
17
+ after do
18
+ Factory.factories.clear
19
+ Factory.sequences.clear
20
+ end
21
+
22
+ describe "after making an instance" do
23
+ before do
24
+ @instance = Factory(:user, :last_name => 'Rye')
25
+ end
26
+
27
+ it "should support a sham called 'name'" do
28
+ @instance.first_name.should == 'Name'
29
+ end
30
+
31
+ it "should use the sham for the email" do
32
+ @instance.email.should =~ /somebody\d@example.com/
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,304 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
2
+
3
+ describe "integration" do
4
+ before do
5
+ Factory.define :user, :class => 'user' do |f|
6
+ f.first_name 'Jimi'
7
+ f.last_name 'Hendrix'
8
+ f.admin false
9
+ f.email {|a| "#{a.first_name}.#{a.last_name}@example.com".downcase }
10
+ end
11
+
12
+ Factory.define Post, :default_strategy => :attributes_for do |f|
13
+ f.name 'Test Post'
14
+ f.association :author, :factory => :user
15
+ end
16
+
17
+ Factory.define :admin, :class => User do |f|
18
+ f.first_name 'Ben'
19
+ f.last_name 'Stein'
20
+ f.admin true
21
+ f.sequence(:username) { |n| "username#{n}" }
22
+ f.email { Factory.next(:email) }
23
+ end
24
+
25
+ Factory.define :sequence_abuser, :class => User do |f|
26
+ f.first_name { Factory.sequence(:email) }
27
+ end
28
+
29
+ Factory.define :guest, :parent => :user do |f|
30
+ f.last_name 'Anonymous'
31
+ f.username 'GuestUser'
32
+ end
33
+
34
+ Factory.define :user_with_callbacks, :parent => :user do |f|
35
+ f.after_stub {|u| u.first_name = 'Stubby' }
36
+ f.after_build {|u| u.first_name = 'Buildy' }
37
+ f.after_create {|u| u.last_name = 'Createy' }
38
+ end
39
+
40
+ Factory.define :user_with_inherited_callbacks, :parent => :user_with_callbacks do |f|
41
+ f.callback(:after_stub) {|u| u.last_name = 'Double-Stubby' }
42
+ end
43
+
44
+ Factory.define :business do |f|
45
+ f.name 'Supplier of Awesome'
46
+ f.association :owner, :factory => :user
47
+ end
48
+
49
+ Factory.sequence :email do |n|
50
+ "somebody#{n}@example.com"
51
+ end
52
+ end
53
+
54
+ after do
55
+ Factory.factories.clear
56
+ end
57
+
58
+ describe "a generated attributes hash" do
59
+
60
+ before do
61
+ @attrs = Factory.attributes_for(:user, :first_name => 'Bill')
62
+ end
63
+
64
+ it "should assign all attributes" do
65
+ expected_attrs = [:admin, :email, :first_name, :last_name]
66
+ actual_attrs = @attrs.keys.sort {|a, b| a.to_s <=> b.to_s }
67
+ actual_attrs.should == expected_attrs
68
+ end
69
+
70
+ it "should correctly assign lazy, dependent attributes" do
71
+ @attrs[:email].should == "bill.hendrix@example.com"
72
+ end
73
+
74
+ it "should override attrbutes" do
75
+ @attrs[:first_name].should == 'Bill'
76
+ end
77
+
78
+ it "should not assign associations" do
79
+ Factory.attributes_for(:post)[:author].should be_nil
80
+ end
81
+
82
+ end
83
+
84
+ describe "a built instance" do
85
+
86
+ before do
87
+ @instance = Factory.build(:post)
88
+ end
89
+
90
+ it "should not be saved" do
91
+ @instance.should be_new_record
92
+ end
93
+
94
+ it "should assign associations" do
95
+ @instance.author.should be_kind_of(User)
96
+ end
97
+
98
+ it "should save associations" do
99
+ @instance.author.should_not be_new_record
100
+ end
101
+
102
+ it "should not assign both an association and its foreign key" do
103
+ Factory.build(:post, :author_id => 1).author_id.should == 1
104
+ end
105
+
106
+ end
107
+
108
+ describe "a created instance" do
109
+
110
+ before do
111
+ @instance = Factory.create('post')
112
+ end
113
+
114
+ it "should be saved" do
115
+ @instance.should_not be_new_record
116
+ end
117
+
118
+ it "should assign associations" do
119
+ @instance.author.should be_kind_of(User)
120
+ end
121
+
122
+ it "should save associations" do
123
+ @instance.author.should_not be_new_record
124
+ end
125
+
126
+ end
127
+
128
+ describe "a generated stub instance" do
129
+
130
+ before do
131
+ @stub = Factory.stub(:user, :first_name => 'Bill')
132
+ end
133
+
134
+ it "should assign all attributes" do
135
+ [:admin, :email, :first_name, :last_name].each do |attr|
136
+ @stub.send(attr).should_not be_nil
137
+ end
138
+ end
139
+
140
+ it "should correctly assign attributes" do
141
+ @stub.email.should == "bill.hendrix@example.com"
142
+ end
143
+
144
+ it "should override attributes" do
145
+ @stub.first_name.should == 'Bill'
146
+ end
147
+
148
+ it "should assign associations" do
149
+ Factory.stub(:post).author.should_not be_nil
150
+ end
151
+
152
+ it "should have an id" do
153
+ @stub.id.should > 0
154
+ end
155
+
156
+ it "should have unique IDs" do
157
+ @other_stub = Factory.stub(:user)
158
+ @stub.id.should_not == @other_stub.id
159
+ end
160
+
161
+ it "should not be considered a new record" do
162
+ @stub.should_not be_new_record
163
+ end
164
+
165
+ it "should raise exception if connection to the database is attempted" do
166
+ lambda { @stub.connection }.should raise_error(RuntimeError)
167
+ lambda { @stub.update_attribute(:first_name, "Nick") }.should raise_error(RuntimeError)
168
+ lambda { @stub.reload }.should raise_error(RuntimeError)
169
+ lambda { @stub.destroy }.should raise_error(RuntimeError)
170
+ lambda { @stub.save }.should raise_error(RuntimeError)
171
+ lambda { @stub.increment!(:age) }.should raise_error(RuntimeError)
172
+ end
173
+ end
174
+
175
+ describe "an instance generated by a factory with a custom class name" do
176
+
177
+ before do
178
+ @instance = Factory.create(:admin)
179
+ end
180
+
181
+ it "should use the correct class name" do
182
+ @instance.should be_kind_of(User)
183
+ end
184
+
185
+ it "should use the correct factory definition" do
186
+ @instance.should be_admin
187
+ end
188
+
189
+ end
190
+
191
+ describe "an instance generated by a factory that inherits from another factory" do
192
+ before do
193
+ @instance = Factory.create(:guest)
194
+ end
195
+
196
+ it "should use the class name of the parent factory" do
197
+ @instance.should be_kind_of(User)
198
+ end
199
+
200
+ it "should have attributes of the parent" do
201
+ @instance.first_name.should == 'Jimi'
202
+ end
203
+
204
+ it "should have attributes defined in the factory itself" do
205
+ @instance.username.should == 'GuestUser'
206
+ end
207
+
208
+ it "should have attributes that have been overriden" do
209
+ @instance.last_name.should == 'Anonymous'
210
+ end
211
+ end
212
+
213
+ describe "an attribute generated by a sequence" do
214
+
215
+ before do
216
+ @email = Factory.attributes_for(:admin)[:email]
217
+ end
218
+
219
+ it "should match the correct format" do
220
+ @email.should =~ /^somebody\d+@example\.com$/
221
+ end
222
+
223
+ describe "after the attribute has already been generated once" do
224
+
225
+ before do
226
+ @another_email = Factory.attributes_for(:admin)[:email]
227
+ end
228
+
229
+ it "should match the correct format" do
230
+ @email.should =~ /^somebody\d+@example\.com$/
231
+ end
232
+
233
+ it "should not be the same as the first generated value" do
234
+ @another_email.should_not == @email
235
+ end
236
+
237
+ end
238
+
239
+ end
240
+
241
+ describe "an attribute generated by an in-line sequence" do
242
+
243
+ before do
244
+ @username = Factory.attributes_for(:admin)[:username]
245
+ end
246
+
247
+ it "should match the correct format" do
248
+ @username.should =~ /^username\d+$/
249
+ end
250
+
251
+ describe "after the attribute has already been generated once" do
252
+
253
+ before do
254
+ @another_username = Factory.attributes_for(:admin)[:username]
255
+ end
256
+
257
+ it "should match the correct format" do
258
+ @username.should =~ /^username\d+$/
259
+ end
260
+
261
+ it "should not be the same as the first generated value" do
262
+ @another_username.should_not == @username
263
+ end
264
+
265
+ end
266
+
267
+ end
268
+
269
+ describe "a factory with a default strategy specified" do
270
+ it "should generate instances according to the strategy" do
271
+ Factory(:post).should be_kind_of(Hash)
272
+ end
273
+ end
274
+
275
+ it "should raise Factory::SequenceAbuseError" do
276
+ lambda {
277
+ Factory(:sequence_abuser)
278
+ }.should raise_error(Factory::SequenceAbuseError)
279
+ end
280
+
281
+ describe "an instance with callbacks" do
282
+ it "should run the after_stub callback when stubbing" do
283
+ @user = Factory.stub(:user_with_callbacks)
284
+ @user.first_name.should == 'Stubby'
285
+ end
286
+
287
+ it "should run the after_build callback when building" do
288
+ @user = Factory.build(:user_with_callbacks)
289
+ @user.first_name.should == 'Buildy'
290
+ end
291
+
292
+ it "should run both the after_build and after_create callbacks when creating" do
293
+ @user = Factory(:user_with_callbacks)
294
+ @user.first_name.should == 'Buildy'
295
+ @user.last_name.should == 'Createy'
296
+ end
297
+
298
+ it "should run both the after_stub callback on the factory and the inherited after_stub callback" do
299
+ @user = Factory.stub(:user_with_inherited_callbacks)
300
+ @user.first_name.should == 'Stubby'
301
+ @user.last_name.should == 'Double-Stubby'
302
+ end
303
+ end
304
+ end