freegenie-factory_girl 1.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CONTRIBUTION_GUIDELINES.rdoc +9 -0
- data/Changelog +29 -0
- data/LICENSE +19 -0
- data/README.rdoc +236 -0
- data/Rakefile +81 -0
- data/lib/factory_girl/aliases.rb +50 -0
- data/lib/factory_girl/attribute/association.rb +20 -0
- data/lib/factory_girl/attribute/dynamic.rb +20 -0
- data/lib/factory_girl/attribute/static.rb +17 -0
- data/lib/factory_girl/attribute.rb +29 -0
- data/lib/factory_girl/factory.rb +412 -0
- data/lib/factory_girl/proxy/attributes_for.rb +21 -0
- data/lib/factory_girl/proxy/build.rb +29 -0
- data/lib/factory_girl/proxy/create.rb +10 -0
- data/lib/factory_girl/proxy/stub.rb +49 -0
- data/lib/factory_girl/proxy.rb +62 -0
- data/lib/factory_girl/sequence.rb +63 -0
- data/lib/factory_girl/syntax/blueprint.rb +42 -0
- data/lib/factory_girl/syntax/generate.rb +68 -0
- data/lib/factory_girl/syntax/make.rb +39 -0
- data/lib/factory_girl/syntax/sham.rb +42 -0
- data/lib/factory_girl/syntax.rb +12 -0
- data/lib/factory_girl.rb +39 -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/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 +525 -0
- data/spec/factory_girl/proxy/attributes_for_spec.rb +52 -0
- data/spec/factory_girl/proxy/build_spec.rb +73 -0
- data/spec/factory_girl/proxy/create_spec.rb +83 -0
- data/spec/factory_girl/proxy/stub_spec.rb +69 -0
- data/spec/factory_girl/proxy_spec.rb +28 -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 +57 -0
- data/spec/factory_girl/syntax/make_spec.rb +35 -0
- data/spec/factory_girl/syntax/sham_spec.rb +35 -0
- data/spec/integration_spec.rb +265 -0
- data/spec/models.rb +43 -0
- data/spec/spec_helper.rb +18 -0
- data/test/factory_girl_test.rb +28 -0
- metadata +118 -0
@@ -0,0 +1,525 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
|
2
|
+
|
3
|
+
describe Factory do
|
4
|
+
describe "defining a factory" do
|
5
|
+
before do
|
6
|
+
@name = :user
|
7
|
+
@factory = "factory"
|
8
|
+
stub(@factory).factory_name { @name }
|
9
|
+
@options = { :class => 'magic' }
|
10
|
+
stub(Factory).new { @factory }
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should create a new factory using the specified name and options" do
|
14
|
+
mock(Factory).new(@name, @options) { @factory }
|
15
|
+
Factory.define(@name, @options) {|f| }
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should pass the factory do the block" do
|
19
|
+
yielded = nil
|
20
|
+
Factory.define(@name) do |y|
|
21
|
+
yielded = y
|
22
|
+
end
|
23
|
+
yielded.should == @factory
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should add the factory to the list of factories" do
|
27
|
+
Factory.define(@name) {|f| }
|
28
|
+
@factory.should == Factory.factories[@name]
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should allow that factory to be found by name" do
|
32
|
+
Factory.factory_by_name(@name).should == @factory
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "a factory" do
|
37
|
+
before do
|
38
|
+
@name = :user
|
39
|
+
@class = User
|
40
|
+
@factory = Factory.new(@name)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should have a factory name" do
|
44
|
+
@factory.factory_name.should == @name
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should have a build class" do
|
48
|
+
@factory.build_class.should == @class
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should have a default strategy" do
|
52
|
+
@factory.default_strategy.should == :create
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should not allow the same attribute to be added twice" do
|
56
|
+
lambda {
|
57
|
+
2.times { @factory.add_attribute :first_name }
|
58
|
+
}.should raise_error(Factory::AttributeDefinitionError)
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should add a static attribute when an attribute is defined with a value" do
|
62
|
+
attribute = 'attribute'
|
63
|
+
stub(attribute).name { :name }
|
64
|
+
mock(Factory::Attribute::Static).new(:name, 'value') { attribute }
|
65
|
+
@factory.add_attribute(:name, 'value')
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should add a dynamic attribute when an attribute is defined with a block" do
|
69
|
+
attribute = 'attribute'
|
70
|
+
stub(attribute).name { :name }
|
71
|
+
block = lambda {}
|
72
|
+
mock(Factory::Attribute::Dynamic).new(:name, block) { attribute }
|
73
|
+
@factory.add_attribute(:name, &block)
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should raise for an attribute with a value and a block" do
|
77
|
+
lambda {
|
78
|
+
@factory.add_attribute(:name, 'value') {}
|
79
|
+
}.should raise_error(Factory::AttributeDefinitionError)
|
80
|
+
end
|
81
|
+
|
82
|
+
describe "adding an attribute using a in-line sequence" do
|
83
|
+
it "should create the sequence" do
|
84
|
+
mock(Factory::Sequence).new
|
85
|
+
@factory.sequence(:name) {}
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should add a dynamic attribute" do
|
89
|
+
attribute = 'attribute'
|
90
|
+
stub(attribute).name { :name }
|
91
|
+
mock(Factory::Attribute::Dynamic).new(:name, is_a(Proc)) { attribute }
|
92
|
+
@factory.sequence(:name) {}
|
93
|
+
@factory.attributes.should include(attribute)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
describe "after adding an attribute" do
|
98
|
+
before do
|
99
|
+
@attribute = "attribute"
|
100
|
+
@proxy = "proxy"
|
101
|
+
|
102
|
+
stub(@attribute).name { :name }
|
103
|
+
stub(@attribute).add_to
|
104
|
+
stub(@proxy).set
|
105
|
+
stub(@proxy).result { 'result' }
|
106
|
+
stub(Factory::Attribute::Static).new { @attribute }
|
107
|
+
stub(Factory::Proxy::Build).new { @proxy }
|
108
|
+
|
109
|
+
@factory.add_attribute(:name, 'value')
|
110
|
+
end
|
111
|
+
|
112
|
+
it "should create the right proxy using the build class when running" do
|
113
|
+
mock(Factory::Proxy::Build).new(@factory.build_class) { @proxy }
|
114
|
+
@factory.run(Factory::Proxy::Build, {})
|
115
|
+
end
|
116
|
+
|
117
|
+
it "should add the attribute to the proxy when running" do
|
118
|
+
mock(@attribute).add_to(@proxy)
|
119
|
+
@factory.run(Factory::Proxy::Build, {})
|
120
|
+
end
|
121
|
+
|
122
|
+
it "should return the result from the proxy when running" do
|
123
|
+
mock(@proxy).result() { 'result' }
|
124
|
+
@factory.run(Factory::Proxy::Build, {}).should == 'result'
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
it "should add an association without a factory name or overrides" do
|
129
|
+
factory = Factory.new(:post)
|
130
|
+
name = :user
|
131
|
+
attr = 'attribute'
|
132
|
+
mock(Factory::Attribute::Association).new(name, name, {}) { attr }
|
133
|
+
factory.association(name)
|
134
|
+
factory.attributes.should include(attr)
|
135
|
+
end
|
136
|
+
|
137
|
+
it "should return associations" do
|
138
|
+
factory = Factory.new(:post)
|
139
|
+
factory.association(:author)
|
140
|
+
factory.association(:editor)
|
141
|
+
factory.associations.each do |association|
|
142
|
+
association.should be_a(Factory::Attribute::Association)
|
143
|
+
end
|
144
|
+
factory.associations.size.should == 2
|
145
|
+
end
|
146
|
+
|
147
|
+
it "should add an association with overrides" do
|
148
|
+
factory = Factory.new(:post)
|
149
|
+
name = :user
|
150
|
+
attr = 'attribute'
|
151
|
+
overrides = { :first_name => 'Ben' }
|
152
|
+
mock(Factory::Attribute::Association).new(name, name, overrides) { attr }
|
153
|
+
factory.association(name, overrides)
|
154
|
+
factory.attributes.should include(attr)
|
155
|
+
end
|
156
|
+
|
157
|
+
it "should add an association with a factory name" do
|
158
|
+
factory = Factory.new(:post)
|
159
|
+
attr = 'attribute'
|
160
|
+
mock(Factory::Attribute::Association).new(:author, :user, {}) { attr }
|
161
|
+
factory.association(:author, :factory => :user)
|
162
|
+
factory.attributes.should include(attr)
|
163
|
+
end
|
164
|
+
|
165
|
+
it "should add an association with a factory name and overrides" do
|
166
|
+
factory = Factory.new(:post)
|
167
|
+
attr = 'attribute'
|
168
|
+
mock(Factory::Attribute::Association).new(:author, :user, :first_name => 'Ben') { attr }
|
169
|
+
factory.association(:author, :factory => :user, :first_name => 'Ben')
|
170
|
+
factory.attributes.should include(attr)
|
171
|
+
end
|
172
|
+
|
173
|
+
it "should raise for a self referencing association" do
|
174
|
+
factory = Factory.new(:post)
|
175
|
+
lambda {
|
176
|
+
factory.association(:parent, :factory => :post)
|
177
|
+
}.should raise_error(Factory::AssociationDefinitionError)
|
178
|
+
end
|
179
|
+
|
180
|
+
it "should add an attribute using the method name when passed an undefined method" do
|
181
|
+
attribute = 'attribute'
|
182
|
+
stub(attribute).name { :name }
|
183
|
+
block = lambda {}
|
184
|
+
mock(Factory::Attribute::Static).new(:name, 'value') { attribute }
|
185
|
+
@factory.send(:name, 'value')
|
186
|
+
@factory.attributes.should include(attribute)
|
187
|
+
end
|
188
|
+
|
189
|
+
it "should allow human_name as a static attribute name" do
|
190
|
+
attribute = 'attribute'
|
191
|
+
stub(attribute).name { :name }
|
192
|
+
mock(Factory::Attribute::Static).new(:human_name, 'value') { attribute}
|
193
|
+
@factory.human_name 'value'
|
194
|
+
end
|
195
|
+
|
196
|
+
it "should allow human_name as a dynamic attribute name" do
|
197
|
+
attribute = 'attribute'
|
198
|
+
stub(attribute).name { :name }
|
199
|
+
block = lambda {}
|
200
|
+
mock(Factory::Attribute::Dynamic).new(:human_name, block) { attribute }
|
201
|
+
@factory.human_name(&block)
|
202
|
+
end
|
203
|
+
|
204
|
+
describe "when overriding generated attributes with a hash" do
|
205
|
+
before do
|
206
|
+
@attr = :name
|
207
|
+
@value = 'The price is right!'
|
208
|
+
@hash = { @attr => @value }
|
209
|
+
end
|
210
|
+
|
211
|
+
it "should return the overridden value in the generated attributes" do
|
212
|
+
@factory.add_attribute(@attr, 'The price is wrong, Bob!')
|
213
|
+
result = @factory.run(Factory::Proxy::AttributesFor, @hash)
|
214
|
+
result[@attr].should == @value
|
215
|
+
end
|
216
|
+
|
217
|
+
it "should not call a lazy attribute block for an overridden attribute" do
|
218
|
+
@factory.add_attribute(@attr) { flunk }
|
219
|
+
result = @factory.run(Factory::Proxy::AttributesFor, @hash)
|
220
|
+
end
|
221
|
+
|
222
|
+
it "should override a symbol parameter with a string parameter" do
|
223
|
+
@factory.add_attribute(@attr, 'The price is wrong, Bob!')
|
224
|
+
@hash = { @attr.to_s => @value }
|
225
|
+
result = @factory.run(Factory::Proxy::AttributesFor, @hash)
|
226
|
+
result[@attr].should == @value
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
describe "overriding an attribute with an alias" do
|
231
|
+
before do
|
232
|
+
@factory.add_attribute(:test, 'original')
|
233
|
+
Factory.alias(/(.*)_alias/, '\1')
|
234
|
+
@result = @factory.run(Factory::Proxy::AttributesFor,
|
235
|
+
:test_alias => 'new')
|
236
|
+
end
|
237
|
+
|
238
|
+
it "should use the passed in value for the alias" do
|
239
|
+
@result[:test_alias].should == 'new'
|
240
|
+
end
|
241
|
+
|
242
|
+
it "should discard the predefined value for the attribute" do
|
243
|
+
@result[:test].should be_nil
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
it "should guess the build class from the factory name" do
|
248
|
+
@factory.build_class.should == User
|
249
|
+
end
|
250
|
+
|
251
|
+
describe "when defined with a custom class" do
|
252
|
+
before do
|
253
|
+
@class = User
|
254
|
+
@factory = Factory.new(:author, :class => @class)
|
255
|
+
end
|
256
|
+
|
257
|
+
it "should use the specified class as the build class" do
|
258
|
+
@factory.build_class.should == @class
|
259
|
+
end
|
260
|
+
end
|
261
|
+
|
262
|
+
describe "when defined with a class instead of a name" do
|
263
|
+
before do
|
264
|
+
@class = ArgumentError
|
265
|
+
@name = :argument_error
|
266
|
+
@factory = Factory.new(@class)
|
267
|
+
end
|
268
|
+
|
269
|
+
it "should guess the name from the class" do
|
270
|
+
@factory.factory_name.should == @name
|
271
|
+
end
|
272
|
+
|
273
|
+
it "should use the class as the build class" do
|
274
|
+
@factory.build_class.should == @class
|
275
|
+
end
|
276
|
+
end
|
277
|
+
|
278
|
+
describe "when defined with a custom class name" do
|
279
|
+
before do
|
280
|
+
@class = ArgumentError
|
281
|
+
@factory = Factory.new(:author, :class => :argument_error)
|
282
|
+
end
|
283
|
+
|
284
|
+
it "should use the specified class as the build class" do
|
285
|
+
@factory.build_class.should == @class
|
286
|
+
end
|
287
|
+
end
|
288
|
+
end
|
289
|
+
|
290
|
+
describe "a factory with a name ending in s" do
|
291
|
+
before do
|
292
|
+
@name = :business
|
293
|
+
@class = Business
|
294
|
+
@factory = Factory.new(@name)
|
295
|
+
end
|
296
|
+
|
297
|
+
it "should have a factory name" do
|
298
|
+
@factory.factory_name.should == @name
|
299
|
+
end
|
300
|
+
|
301
|
+
it "should have a build class" do
|
302
|
+
@factory.build_class.should == @class
|
303
|
+
end
|
304
|
+
end
|
305
|
+
|
306
|
+
describe "a factory with a string for a name" do
|
307
|
+
before do
|
308
|
+
@name = :user
|
309
|
+
@factory = Factory.new(@name.to_s) {}
|
310
|
+
end
|
311
|
+
|
312
|
+
it "should convert the string to a symbol" do
|
313
|
+
@factory.factory_name.should == @name
|
314
|
+
end
|
315
|
+
end
|
316
|
+
|
317
|
+
describe "a factory defined with a string name" do
|
318
|
+
before do
|
319
|
+
Factory.factories = {}
|
320
|
+
@name = :user
|
321
|
+
@factory = Factory.define(@name.to_s) {}
|
322
|
+
end
|
323
|
+
|
324
|
+
it "should store the factory using a symbol" do
|
325
|
+
Factory.factories[@name].should == @factory
|
326
|
+
end
|
327
|
+
end
|
328
|
+
|
329
|
+
describe "after defining a factory" do
|
330
|
+
before do
|
331
|
+
@name = :user
|
332
|
+
@factory = "factory"
|
333
|
+
|
334
|
+
Factory.factories[@name] = @factory
|
335
|
+
end
|
336
|
+
|
337
|
+
after { Factory.factories.clear }
|
338
|
+
|
339
|
+
it "should use Proxy::AttributesFor for Factory.attributes_for" do
|
340
|
+
mock(@factory).run(Factory::Proxy::AttributesFor, :attr => 'value') { 'result' }
|
341
|
+
Factory.attributes_for(@name, :attr => 'value').should == 'result'
|
342
|
+
end
|
343
|
+
|
344
|
+
it "should use Proxy::Build for Factory.build" do
|
345
|
+
mock(@factory).run(Factory::Proxy::Build, :attr => 'value') { 'result' }
|
346
|
+
Factory.build(@name, :attr => 'value').should == 'result'
|
347
|
+
end
|
348
|
+
|
349
|
+
it "should use Proxy::Create for Factory.create" do
|
350
|
+
mock(@factory).run(Factory::Proxy::Create, :attr => 'value') { 'result' }
|
351
|
+
Factory.create(@name, :attr => 'value').should == 'result'
|
352
|
+
end
|
353
|
+
|
354
|
+
it "should use Proxy::Stub for Factory.stub" do
|
355
|
+
mock(@factory).run(Factory::Proxy::Stub, :attr => 'value') { 'result' }
|
356
|
+
Factory.stub(@name, :attr => 'value').should == 'result'
|
357
|
+
end
|
358
|
+
|
359
|
+
it "should use default strategy option as Factory.default_strategy" do
|
360
|
+
stub(@factory).default_strategy { :create }
|
361
|
+
mock(@factory).run(Factory::Proxy::Create, :attr => 'value') { 'result' }
|
362
|
+
Factory.default_strategy(@name, :attr => 'value').should == 'result'
|
363
|
+
end
|
364
|
+
|
365
|
+
it "should use the default strategy for the global Factory method" do
|
366
|
+
stub(@factory).default_strategy { :create }
|
367
|
+
mock(@factory).run(Factory::Proxy::Create, :attr => 'value') { 'result' }
|
368
|
+
Factory(@name, :attr => 'value').should == 'result'
|
369
|
+
end
|
370
|
+
|
371
|
+
[:build, :create, :attributes_for, :stub].each do |method|
|
372
|
+
it "should raise an ArgumentError on #{method} with a nonexistant factory" do
|
373
|
+
lambda { Factory.send(method, :bogus) }.should raise_error(ArgumentError)
|
374
|
+
end
|
375
|
+
|
376
|
+
it "should recognize either 'name' or :name for Factory.#{method}" do
|
377
|
+
stub(@factory).run
|
378
|
+
lambda { Factory.send(method, @name.to_s) }.should_not raise_error
|
379
|
+
lambda { Factory.send(method, @name.to_sym) }.should_not raise_error
|
380
|
+
end
|
381
|
+
end
|
382
|
+
end
|
383
|
+
|
384
|
+
describe 'defining a factory with a parent parameter' do
|
385
|
+
before do
|
386
|
+
@parent = Factory.define :object do |f|
|
387
|
+
f.name 'Name'
|
388
|
+
end
|
389
|
+
end
|
390
|
+
|
391
|
+
it "should raise an ArgumentError when trying to use a non-existent factory as parent" do
|
392
|
+
lambda {
|
393
|
+
Factory.define(:child, :parent => :nonexsitent) {}
|
394
|
+
}.should raise_error(ArgumentError)
|
395
|
+
end
|
396
|
+
|
397
|
+
it "should create a new factory using the class of the parent" do
|
398
|
+
child = Factory.define(:child, :parent => :object) {}
|
399
|
+
child.build_class.should == @parent.build_class
|
400
|
+
end
|
401
|
+
|
402
|
+
it "should create a new factory while overriding the parent class" do
|
403
|
+
class Other; end
|
404
|
+
|
405
|
+
child = Factory.define(:child, :parent => :object, :class => Other) {}
|
406
|
+
child.build_class.should == Other
|
407
|
+
end
|
408
|
+
|
409
|
+
it "should create a new factory with attributes of the parent" do
|
410
|
+
child = Factory.define(:child, :parent => :object) {}
|
411
|
+
child.attributes.size.should == 1
|
412
|
+
child.attributes.first.name.should == :name
|
413
|
+
end
|
414
|
+
|
415
|
+
it "should allow to define additional attributes" do
|
416
|
+
child = Factory.define(:child, :parent => :object) do |f|
|
417
|
+
f.email 'person@somebody.com'
|
418
|
+
end
|
419
|
+
child.attributes.size.should == 2
|
420
|
+
end
|
421
|
+
|
422
|
+
it "should allow to override parent attributes" do
|
423
|
+
child = Factory.define(:child, :parent => :object) do |f|
|
424
|
+
f.name { 'Child Name' }
|
425
|
+
end
|
426
|
+
child.attributes.size.should == 1
|
427
|
+
child.attributes.first.should be_kind_of(Factory::Attribute::Dynamic)
|
428
|
+
end
|
429
|
+
end
|
430
|
+
|
431
|
+
describe 'defining a factory with a default strategy parameter' do
|
432
|
+
it "should raise an ArgumentError when trying to use a non-existent factory" do
|
433
|
+
lambda {
|
434
|
+
Factory.define(:object, :default_strategy => :nonexistent) {}
|
435
|
+
}.should raise_error(ArgumentError)
|
436
|
+
end
|
437
|
+
|
438
|
+
it "should create a new factory with a specified default strategy" do
|
439
|
+
factory = Factory.define(:object, :default_strategy => :stub) {}
|
440
|
+
factory.default_strategy.should == :stub
|
441
|
+
end
|
442
|
+
end
|
443
|
+
|
444
|
+
def self.in_directory_with_files(*files)
|
445
|
+
before do
|
446
|
+
@pwd = Dir.pwd
|
447
|
+
@tmp_dir = File.join(File.dirname(__FILE__), 'tmp')
|
448
|
+
FileUtils.mkdir_p @tmp_dir
|
449
|
+
Dir.chdir(@tmp_dir)
|
450
|
+
|
451
|
+
files.each do |file|
|
452
|
+
FileUtils.mkdir_p File.dirname(file)
|
453
|
+
FileUtils.touch file
|
454
|
+
stub(Factory).require(file)
|
455
|
+
end
|
456
|
+
end
|
457
|
+
|
458
|
+
after do
|
459
|
+
Dir.chdir(@pwd)
|
460
|
+
FileUtils.rm_rf(@tmp_dir)
|
461
|
+
end
|
462
|
+
end
|
463
|
+
|
464
|
+
def require_definitions_from(file)
|
465
|
+
simple_matcher do |given, matcher|
|
466
|
+
has_received = have_received.require(file)
|
467
|
+
result = has_received.matches?(given)
|
468
|
+
matcher.description = "require definitions from #{file}"
|
469
|
+
matcher.failure_message = has_received.failure_message
|
470
|
+
result
|
471
|
+
end
|
472
|
+
end
|
473
|
+
|
474
|
+
share_examples_for "finds definitions" do
|
475
|
+
before do
|
476
|
+
stub(Factory).require
|
477
|
+
Factory.find_definitions
|
478
|
+
end
|
479
|
+
subject { Factory }
|
480
|
+
end
|
481
|
+
|
482
|
+
describe "with factories.rb" do
|
483
|
+
in_directory_with_files 'factories.rb'
|
484
|
+
it_should_behave_like "finds definitions"
|
485
|
+
it { should require_definitions_from('factories.rb') }
|
486
|
+
end
|
487
|
+
|
488
|
+
%w(spec test).each do |dir|
|
489
|
+
describe "with a factories file under #{dir}" do
|
490
|
+
in_directory_with_files File.join(dir, 'factories.rb')
|
491
|
+
it_should_behave_like "finds definitions"
|
492
|
+
it { should require_definitions_from("#{dir}/factories.rb") }
|
493
|
+
end
|
494
|
+
|
495
|
+
describe "with a factories file under #{dir}/factories" do
|
496
|
+
in_directory_with_files File.join(dir, 'factories', 'post_factory.rb')
|
497
|
+
it_should_behave_like "finds definitions"
|
498
|
+
it { should require_definitions_from("#{dir}/factories/post_factory.rb") }
|
499
|
+
end
|
500
|
+
|
501
|
+
describe "with several factories files under #{dir}/factories" do
|
502
|
+
in_directory_with_files File.join(dir, 'factories', 'post_factory.rb'),
|
503
|
+
File.join(dir, 'factories', 'person_factory.rb')
|
504
|
+
it_should_behave_like "finds definitions"
|
505
|
+
it { should require_definitions_from("#{dir}/factories/post_factory.rb") }
|
506
|
+
it { should require_definitions_from("#{dir}/factories/person_factory.rb") }
|
507
|
+
end
|
508
|
+
|
509
|
+
describe "with nested and unnested factories files under #{dir}" do
|
510
|
+
in_directory_with_files File.join(dir, 'factories.rb'),
|
511
|
+
File.join(dir, 'factories', 'post_factory.rb'),
|
512
|
+
File.join(dir, 'factories', 'person_factory.rb')
|
513
|
+
it_should_behave_like "finds definitions"
|
514
|
+
it { should require_definitions_from("#{dir}/factories.rb") }
|
515
|
+
it { should require_definitions_from("#{dir}/factories/post_factory.rb") }
|
516
|
+
it { should require_definitions_from("#{dir}/factories/person_factory.rb") }
|
517
|
+
end
|
518
|
+
end
|
519
|
+
|
520
|
+
it "should return the factory name without underscores for the human name" do
|
521
|
+
factory = Factory.new(:name_with_underscores)
|
522
|
+
factory.human_name.should == 'name with underscores'
|
523
|
+
end
|
524
|
+
|
525
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
|
2
|
+
|
3
|
+
describe Factory::Proxy::AttributesFor do
|
4
|
+
before do
|
5
|
+
@proxy = Factory::Proxy::AttributesFor.new(@class)
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "when asked to associate with another factory" do
|
9
|
+
before do
|
10
|
+
stub(Factory).create
|
11
|
+
@proxy.associate(:owner, :user, {})
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should not set a value for the association" do
|
15
|
+
(@proxy.result.key?(:owner)).should_not be
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should return nil when building an association" do
|
20
|
+
@proxy.association(:user).should be_nil
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should not call Factory.create when building an association" do
|
24
|
+
stub(Factory).create
|
25
|
+
@proxy.association(:user).should be_nil
|
26
|
+
Factory.should have_received.create.never
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should always return nil when building an association" do
|
30
|
+
@proxy.set(:association, 'x')
|
31
|
+
@proxy.association(:user).should be_nil
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should return a hash when asked for the result" do
|
35
|
+
@proxy.result.should be_kind_of(Hash)
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "after setting an attribute" do
|
39
|
+
before do
|
40
|
+
@proxy.set(:attribute, 'value')
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should set that value in the resulting hash" do
|
44
|
+
@proxy.result[:attribute].should == 'value'
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should return that value when asked for that attribute" do
|
48
|
+
@proxy.get(:attribute).should == 'value'
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
|
2
|
+
|
3
|
+
describe Factory::Proxy::Build do
|
4
|
+
before do
|
5
|
+
@class = Class.new
|
6
|
+
@instance = "built-instance"
|
7
|
+
@association = "associated-instance"
|
8
|
+
|
9
|
+
stub(@class).new { @instance }
|
10
|
+
stub(@instance).attribute { 'value' }
|
11
|
+
stub(Factory).create { @association }
|
12
|
+
stub(@instance, :attribute=)
|
13
|
+
stub(@instance, :owner=)
|
14
|
+
|
15
|
+
@proxy = Factory::Proxy::Build.new(@class)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should instantiate the class" do
|
19
|
+
@class.should have_received.new
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "when asked to associate with another factory" do
|
23
|
+
before do
|
24
|
+
@proxy.associate(:owner, :user, {})
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should create the associated instance" do
|
28
|
+
Factory.should have_received.create(:user, {})
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should set the associated instance" do
|
32
|
+
@instance.should have_received.method_missing(:owner=, @association)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should call Factory.create when building an association" do
|
37
|
+
association = 'association'
|
38
|
+
attribs = { :first_name => 'Billy' }
|
39
|
+
stub(Factory).create { association }
|
40
|
+
@proxy.association(:user, attribs).should == association
|
41
|
+
Factory.should have_received.create(:user, attribs)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should return the built instance when asked for the result" do
|
45
|
+
@proxy.result.should == @instance
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "when setting an attribute" do
|
49
|
+
before do
|
50
|
+
stub(@instance).attribute = 'value'
|
51
|
+
@proxy.set(:attribute, 'value')
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should set that value" do
|
55
|
+
@instance.should have_received.method_missing(:attribute=, 'value')
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "when getting an attribute" do
|
60
|
+
before do
|
61
|
+
@result = @proxy.get(:attribute)
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should ask the built class for the value" do
|
65
|
+
@instance.should have_received.attribute
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should return the value for that attribute" do
|
69
|
+
@result.should == 'value'
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|