masa-iwasaki-factory_girl 1.2.1.1 → 1.2.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +56 -9
- data/Rakefile +19 -11
- data/lib/factory_girl.rb +2 -0
- data/lib/factory_girl/attribute/association.rb +2 -0
- data/lib/factory_girl/attribute/callback.rb +16 -0
- data/lib/factory_girl/attribute/dynamic.rb +1 -1
- data/lib/factory_girl/factory.rb +64 -44
- data/lib/factory_girl/proxy.rb +19 -0
- data/lib/factory_girl/proxy/build.rb +1 -0
- data/lib/factory_girl/proxy/create.rb +53 -1
- data/lib/factory_girl/proxy/stub.rb +7 -6
- data/lib/factory_girl/step_definitions.rb +54 -0
- data/spec/factory_girl/aliases_spec.rb +29 -0
- data/spec/factory_girl/attribute/association_spec.rb +29 -0
- data/spec/factory_girl/attribute/callback_spec.rb +23 -0
- data/spec/factory_girl/attribute/dynamic_spec.rb +49 -0
- data/spec/factory_girl/attribute/static_spec.rb +29 -0
- data/spec/factory_girl/attribute_spec.rb +30 -0
- data/spec/factory_girl/factory_spec.rb +582 -0
- data/spec/factory_girl/proxy/attributes_for_spec.rb +52 -0
- data/spec/factory_girl/proxy/build_spec.rb +81 -0
- data/spec/factory_girl/proxy/create_spec.rb +97 -0
- data/spec/factory_girl/proxy/stub_spec.rb +79 -0
- data/spec/factory_girl/proxy_spec.rb +84 -0
- data/spec/factory_girl/sequence_spec.rb +66 -0
- data/spec/factory_girl/syntax/blueprint_spec.rb +35 -0
- data/spec/factory_girl/syntax/generate_spec.rb +58 -0
- data/spec/factory_girl/syntax/make_spec.rb +35 -0
- data/spec/factory_girl/syntax/sham_spec.rb +36 -0
- data/spec/integration_spec.rb +304 -0
- data/spec/models.rb +43 -0
- data/spec/spec_helper.rb +18 -0
- metadata +49 -9
@@ -0,0 +1,35 @@
|
|
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.clear_caches
|
8
|
+
Factory.sequence(:email) { |n| "somebody#{n}@example.com" }
|
9
|
+
User.blueprint do
|
10
|
+
first_name { 'Bill' }
|
11
|
+
last_name { 'Nye' }
|
12
|
+
email { Factory.next(:email) }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
after do
|
17
|
+
Factory.factories.clear
|
18
|
+
Factory.sequences.clear
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "after making an instance" do
|
22
|
+
before do
|
23
|
+
@instance = Factory.create(:user, :last_name => 'Rye')
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should use attributes from the blueprint" do
|
27
|
+
@instance.first_name.should == 'Bill'
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should evaluate attribute blocks for each instance" do
|
31
|
+
@instance.email.should =~ /somebody\d+@example.com/
|
32
|
+
Factory(:user).email.should_not == @instance.email
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,58 @@
|
|
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.clear_caches
|
8
|
+
Factory.define :user do |factory|
|
9
|
+
factory.first_name 'Bill'
|
10
|
+
factory.last_name 'Nye'
|
11
|
+
factory.email 'science@guys.net'
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
after do
|
16
|
+
Factory.factories.clear
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should raise an error even if when generating an invalid instance" do
|
20
|
+
lambda { User.generate(:first_name => nil) }.should_not raise_error
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should raise an error when forcefully generating an invalid instance" do
|
24
|
+
lambda { User.generate!(:first_name => nil) }.should raise_error
|
25
|
+
end
|
26
|
+
|
27
|
+
%w(generate generate! spawn).each do |method|
|
28
|
+
it "should yield a generated instance when using #{method} with a block" do
|
29
|
+
saved_instance = nil
|
30
|
+
User.send(method) {|instance| saved_instance = instance }
|
31
|
+
saved_instance.should be_kind_of(User)
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "after generating an instance using #{method}" do
|
35
|
+
before do
|
36
|
+
@instance = User.send(method, :last_name => 'Rye')
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should use attributes from the factory" do
|
40
|
+
@instance.first_name.should == 'Bill'
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should use attributes passed to generate" do
|
44
|
+
@instance.last_name.should == 'Rye'
|
45
|
+
end
|
46
|
+
|
47
|
+
if method == 'spawn'
|
48
|
+
it "should not save the record" do
|
49
|
+
@instance.should be_new_record
|
50
|
+
end
|
51
|
+
else
|
52
|
+
it "should save the record" do
|
53
|
+
@instance.should_not be_new_record
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
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,36 @@
|
|
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
|
+
Factory.clear_caches
|
8
|
+
Sham.name { "Name" }
|
9
|
+
Sham.email { "somebody#{rand(5)}@example.com" }
|
10
|
+
|
11
|
+
Factory.define :user do |factory|
|
12
|
+
factory.first_name { Sham.name }
|
13
|
+
factory.last_name { Sham.name }
|
14
|
+
factory.email { Sham.email }
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
after do
|
19
|
+
Factory.factories.clear
|
20
|
+
Factory.sequences.clear
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "after making an instance" do
|
24
|
+
before do
|
25
|
+
@instance = Factory.create(:user, :last_name => 'Rye')
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should support a sham called 'name'" do
|
29
|
+
@instance.first_name.should == 'Name'
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should use the sham for the email" do
|
33
|
+
@instance.email.should =~ /somebody\d@example.com/
|
34
|
+
end
|
35
|
+
end
|
36
|
+
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
|