malvestuto_factory_girl 1.2.5

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