factory_girl 2.0.4 → 2.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. data/CONTRIBUTION_GUIDELINES.md +1 -0
  2. data/GETTING_STARTED.md +28 -4
  3. data/Gemfile +2 -1
  4. data/Gemfile.lock +6 -2
  5. data/features/factory_girl_steps.feature +17 -0
  6. data/features/step_definitions/database_steps.rb +22 -0
  7. data/features/support/factories.rb +13 -0
  8. data/features/support/test.db +0 -0
  9. data/lib/factory_girl/aliases.rb +1 -3
  10. data/lib/factory_girl/attribute.rb +16 -8
  11. data/lib/factory_girl/attribute/association.rb +0 -3
  12. data/lib/factory_girl/attribute/callback.rb +0 -2
  13. data/lib/factory_girl/attribute/dynamic.rb +1 -3
  14. data/lib/factory_girl/attribute/static.rb +1 -1
  15. data/lib/factory_girl/attribute_list.rb +30 -9
  16. data/lib/factory_girl/factory.rb +1 -1
  17. data/lib/factory_girl/proxy.rb +7 -5
  18. data/lib/factory_girl/proxy/attributes_for.rb +8 -3
  19. data/lib/factory_girl/proxy/build.rb +12 -3
  20. data/lib/factory_girl/proxy/stub.rb +12 -3
  21. data/lib/factory_girl/sequence.rb +2 -2
  22. data/lib/factory_girl/step_definitions.rb +3 -2
  23. data/lib/factory_girl/version.rb +1 -1
  24. data/spec/acceptance/attribute_aliases_spec.rb +0 -1
  25. data/spec/acceptance/attributes_for_spec.rb +0 -1
  26. data/spec/acceptance/attributes_ordered_spec.rb +24 -6
  27. data/spec/acceptance/build_list_spec.rb +0 -1
  28. data/spec/acceptance/build_spec.rb +0 -1
  29. data/spec/acceptance/build_stubbed_spec.rb +0 -1
  30. data/spec/acceptance/callbacks_spec.rb +0 -1
  31. data/spec/acceptance/create_list_spec.rb +0 -1
  32. data/spec/acceptance/create_spec.rb +0 -1
  33. data/spec/acceptance/default_strategy_spec.rb +0 -1
  34. data/spec/acceptance/definition_spec.rb +0 -1
  35. data/spec/acceptance/definition_without_block_spec.rb +0 -1
  36. data/spec/acceptance/overrides_spec.rb +0 -1
  37. data/spec/acceptance/parent_spec.rb +18 -1
  38. data/spec/acceptance/sequence_spec.rb +0 -1
  39. data/spec/acceptance/syntax/blueprint_spec.rb +0 -1
  40. data/spec/acceptance/syntax/generate_spec.rb +0 -1
  41. data/spec/acceptance/syntax/make_spec.rb +0 -1
  42. data/spec/acceptance/syntax/sham_spec.rb +0 -1
  43. data/spec/acceptance/syntax/vintage_spec.rb +27 -29
  44. data/spec/acceptance/traits_spec.rb +0 -1
  45. data/spec/acceptance/transient_attributes_spec.rb +68 -0
  46. data/spec/factory_girl/aliases_spec.rb +19 -21
  47. data/spec/factory_girl/attribute/association_spec.rb +16 -24
  48. data/spec/factory_girl/attribute/callback_spec.rb +14 -15
  49. data/spec/factory_girl/attribute/dynamic_spec.rb +41 -45
  50. data/spec/factory_girl/attribute/implicit_spec.rb +18 -30
  51. data/spec/factory_girl/attribute/sequence_spec.rb +11 -13
  52. data/spec/factory_girl/attribute/static_spec.rb +13 -20
  53. data/spec/factory_girl/attribute_list_spec.rb +9 -1
  54. data/spec/factory_girl/attribute_spec.rb +17 -28
  55. data/spec/factory_girl/definition_proxy_spec.rb +131 -82
  56. data/spec/factory_girl/deprecated_spec.rb +15 -36
  57. data/spec/factory_girl/factory_spec.rb +106 -135
  58. data/spec/factory_girl/find_definitions_spec.rb +7 -6
  59. data/spec/factory_girl/proxy/attributes_for_spec.rb +23 -32
  60. data/spec/factory_girl/proxy/build_spec.rb +6 -80
  61. data/spec/factory_girl/proxy/create_spec.rb +24 -89
  62. data/spec/factory_girl/proxy/stub_spec.rb +14 -73
  63. data/spec/factory_girl/proxy_spec.rb +53 -53
  64. data/spec/factory_girl/registry_spec.rb +13 -29
  65. data/spec/factory_girl/sequence_spec.rb +24 -72
  66. data/spec/factory_girl_spec.rb +10 -5
  67. data/spec/spec_helper.rb +5 -79
  68. data/spec/support/macros/define_constant.rb +86 -0
  69. data/spec/support/shared_examples/proxy.rb +99 -0
  70. metadata +34 -20
  71. data/spec/acceptance/acceptance_helper.rb +0 -11
@@ -12,6 +12,11 @@ describe FactoryGirl::AttributeList, "#define_attribute" do
12
12
  subject.to_a.should == [static_attribute, dynamic_attribute]
13
13
  end
14
14
 
15
+ it "returns the attribute" do
16
+ subject.define_attribute(static_attribute).should == static_attribute
17
+ subject.define_attribute(dynamic_attribute).should == dynamic_attribute
18
+ end
19
+
15
20
  it "raises if an attribute has already been defined" do
16
21
  expect {
17
22
  2.times { subject.define_attribute(static_attribute) }
@@ -49,7 +54,6 @@ describe FactoryGirl::AttributeList, "#add_callback" do
49
54
  let(:valid_callback_names) { [:after_create, :after_build, :after_stub] }
50
55
  let(:invalid_callback_names) { [:before_create, :before_build, :bogus] }
51
56
 
52
-
53
57
  it "allows for defining adding a callback" do
54
58
  subject.add_callback(:after_create) { "Called after_create" }
55
59
 
@@ -59,6 +63,10 @@ describe FactoryGirl::AttributeList, "#add_callback" do
59
63
  proxy.callbacks[:after_create].first.call.should == "Called after_create"
60
64
  end
61
65
 
66
+ it "returns the callback" do
67
+ subject.add_callback(:after_create) { "Called after_create" }.should be_a(FactoryGirl::Attribute::Callback)
68
+ end
69
+
62
70
  it "allows valid callback names to be assigned" do
63
71
  valid_callback_names.each do |callback_name|
64
72
  expect do
@@ -1,43 +1,32 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe FactoryGirl::Attribute do
4
- before do
5
- @name = :user
6
- @attr = FactoryGirl::Attribute.new(@name)
7
- end
8
-
9
- it "should have a name" do
10
- @attr.name.should == @name
11
- end
4
+ let(:name) { "user" }
5
+ let(:proxy) { stub("proxy") }
6
+ subject { FactoryGirl::Attribute.new(name) }
12
7
 
13
- it "isn't an association" do
14
- @attr.should_not be_association
15
- end
8
+ its(:name) { should == name.to_sym }
9
+ it { should_not be_association }
16
10
 
17
- it "should do nothing when being added to a proxy" do
18
- @proxy = "proxy"
19
- stub(@proxy).set
20
- @attr.add_to(@proxy)
21
- @proxy.should have_received.set.never
11
+ it "doesn't set any attributes on a proxy when added" do
12
+ proxy.stubs(:set)
13
+ subject.add_to(proxy)
14
+ proxy.should have_received(:set).never
22
15
  end
23
16
 
24
- it "should raise an error when defining an attribute writer" do
17
+ it "raises an error when defining an attribute writer" do
25
18
  error_message = %{factory_girl uses 'f.test value' syntax rather than 'f.test = value'}
26
- lambda {
19
+ expect {
27
20
  FactoryGirl::Attribute.new('test=')
28
- }.should raise_error(FactoryGirl::AttributeDefinitionError, error_message)
29
- end
30
-
31
- it "should convert names to symbols" do
32
- FactoryGirl::Attribute.new('name').name.should == :name
21
+ }.to raise_error(FactoryGirl::AttributeDefinitionError, error_message)
33
22
  end
34
23
 
35
- it "should return nil when is compared with a non-attribute object" do
36
- (@attr <=> "foo").should == nil
24
+ it "returns nil when compared to a non-attribute" do
25
+ (subject <=> "foo").should be_nil
37
26
  end
38
27
 
39
- it "should use priority to perform comparisons" do
40
- attr2 = FactoryGirl::Attribute.new('name')
41
- (@attr <=> attr2).should == 0
28
+ it "uses priority to perform comparisons" do
29
+ second_attribute = FactoryGirl::Attribute.new('name')
30
+ (subject <=> second_attribute).should be_zero
42
31
  end
43
32
  end
@@ -15,20 +15,22 @@ describe FactoryGirl::DefinitionProxy do
15
15
  end
16
16
 
17
17
  it "should add a static attribute when an attribute is defined with a value" do
18
- attribute = 'attribute'
19
- stub(attribute).name { :name }
20
- mock(FactoryGirl::Attribute::Static).new(:name, 'value') { attribute }
21
- mock(factory).define_attribute(attribute)
18
+ attribute = stub('attribute', :name => :name)
19
+ FactoryGirl::Attribute::Static.stubs(:new => attribute)
20
+ factory.stubs(:define_attribute)
22
21
  subject.add_attribute(:name, 'value')
22
+ factory.should have_received(:define_attribute).with(attribute)
23
+ FactoryGirl::Attribute::Static.should have_received(:new).with(:name, "value")
23
24
  end
24
25
 
25
26
  it "should add a dynamic attribute when an attribute is defined with a block" do
26
- attribute = 'attribute'
27
- stub(attribute).name { :name }
27
+ attribute = stub('attribute', :name => :name)
28
28
  block = lambda {}
29
- mock(FactoryGirl::Attribute::Dynamic).new(:name, block) { attribute }
30
- mock(factory).define_attribute(attribute)
29
+ FactoryGirl::Attribute::Dynamic.stubs(:new => attribute)
30
+ factory.stubs(:define_attribute)
31
31
  subject.add_attribute(:name, &block)
32
+ FactoryGirl::Attribute::Dynamic.should have_received(:new).with(:name, block)
33
+ factory.should have_received(:define_attribute).with(attribute)
32
34
  end
33
35
 
34
36
  it "should raise for an attribute with a value and a block" do
@@ -54,103 +56,150 @@ describe FactoryGirl::DefinitionProxy do
54
56
 
55
57
  describe "adding an attribute using a in-line sequence" do
56
58
  it "should create the sequence" do
57
- mock(FactoryGirl::Sequence).new(:name, 1)
59
+ FactoryGirl::Sequence.stubs(:new)
58
60
  subject.sequence(:name) {}
61
+ FactoryGirl::Sequence.should have_received(:new).with(:name, 1)
59
62
  end
60
63
 
61
64
  it "should create the sequence with a custom default value" do
62
- mock(FactoryGirl::Sequence).new(:name, "A")
65
+ FactoryGirl::Sequence.stubs(:new)
63
66
  subject.sequence(:name, "A") {}
67
+ FactoryGirl::Sequence.should have_received(:new).with(:name, "A")
64
68
  end
69
+ end
65
70
 
66
- it "should add a dynamic attribute" do
67
- attribute = 'attribute'
68
- stub(attribute).name { :name }
69
- mock(FactoryGirl::Attribute::Dynamic).new(:name, is_a(Proc)) { attribute }
70
- subject.sequence(:name) {}
71
- factory.attributes.should include(attribute)
71
+ it "adds an implicit attribute when passed an undefined method without arguments or a block" do
72
+ factory.stubs(:define_attribute)
73
+ attribute = stub('attribute', :name => :name)
74
+ FactoryGirl::Attribute::Implicit.stubs(:new => attribute)
75
+ subject.send(:name)
76
+ FactoryGirl::Attribute::Implicit.should have_received(:new).with(:name, factory)
77
+ factory.should have_received(:define_attribute).with(attribute)
78
+ end
79
+ end
80
+
81
+ describe FactoryGirl::DefinitionProxy, "with a factory mock" do
82
+ before do
83
+ define_class("FactoryMock") do
84
+ def add_callback(callback, &block)
85
+ [callback, block.call]
86
+ end
87
+
88
+ def to_create(&block)
89
+ block.call
90
+ end
72
91
  end
73
92
  end
74
93
 
75
- it "should add a callback attribute when the after_build attribute is defined" do
76
- mock(FactoryGirl::Attribute::Callback).new(:after_build, is_a(Proc)) { 'after_build callback' }
77
- subject.after_build {}
78
- factory.attributes.should include('after_build callback')
94
+ let(:factory_mock) { FactoryMock.new }
95
+ subject { FactoryGirl::DefinitionProxy.new(factory_mock) }
96
+
97
+ it "defines after_build callbacks" do
98
+ subject.after_build { "after_build value" }.should == [:after_build, "after_build value"]
99
+ end
100
+
101
+ it "defines after_create callbacks" do
102
+ subject.after_create { "after_create value" }.should == [:after_create, "after_create value"]
103
+ end
104
+
105
+ it "defines after_stub callbacks" do
106
+ subject.after_stub { "after_stub value" }.should == [:after_stub, "after_stub value"]
79
107
  end
80
108
 
81
- it "should add a callback attribute when the after_create attribute is defined" do
82
- mock(FactoryGirl::Attribute::Callback).new(:after_create, is_a(Proc)) { 'after_create callback' }
83
- subject.after_create {}
84
- factory.attributes.should include('after_create callback')
109
+ it "defines to_create" do
110
+ subject.to_create { "to_create value" }.should == "to_create value"
85
111
  end
112
+ end
113
+
114
+ describe FactoryGirl::DefinitionProxy, "adding attributes" do
115
+ let(:factory) { FactoryGirl::Factory.new(:object) }
116
+ subject { FactoryGirl::DefinitionProxy.new(factory) }
117
+ let(:attribute) { stub("created attribute") }
118
+ let(:block) { lambda { } }
119
+ let(:attribute_name) { :full_name }
120
+ let(:attribute_value) { "passed value" }
121
+
122
+ before { factory.stubs(:define_attribute) }
123
+
124
+ context "when a block is passed" do
125
+ before { FactoryGirl::Attribute::Dynamic.stubs(:new => attribute) }
126
+
127
+ it "creates a dynamic attribute" do
128
+ subject.add_attribute(attribute_name, &block)
129
+ FactoryGirl::Attribute::Dynamic.should have_received(:new).with(attribute_name, block)
130
+ factory.should have_received(:define_attribute).with(attribute)
131
+ end
86
132
 
87
- it "should add a callback attribute when the after_stub attribute is defined" do
88
- mock(FactoryGirl::Attribute::Callback).new(:after_stub, is_a(Proc)) { 'after_stub callback' }
89
- subject.after_stub {}
90
- factory.attributes.should include('after_stub callback')
133
+ it "creates a dynamic attribute without the method being defined" do
134
+ subject.send(attribute_name, &block)
135
+ FactoryGirl::Attribute::Dynamic.should have_received(:new).with(attribute_name, block)
136
+ factory.should have_received(:define_attribute).with(attribute)
137
+ end
91
138
  end
92
139
 
93
- it "should add an association without a factory name or overrides" do
94
- name = :user
95
- attr = 'attribute'
96
- stub(attr).name { name }
97
- mock(FactoryGirl::Attribute::Association).new(name, name, {}) { attr }
98
- subject.association(name)
99
- factory.attributes.should include(attr)
140
+ context "when a value is passed" do
141
+ before { FactoryGirl::Attribute::Static.stubs(:new => attribute) }
142
+
143
+ it "creates a static attribute" do
144
+ subject.add_attribute(attribute_name, attribute_value)
145
+ FactoryGirl::Attribute::Static.should have_received(:new).with(attribute_name, attribute_value)
146
+ factory.should have_received(:define_attribute).with(attribute)
147
+ end
148
+
149
+ it "creates a static attribute without the method being defined" do
150
+ subject.send(attribute_name, attribute_value)
151
+ FactoryGirl::Attribute::Static.should have_received(:new).with(attribute_name, attribute_value)
152
+ factory.should have_received(:define_attribute).with(attribute)
153
+ end
100
154
  end
101
155
 
102
- it "should add an association with overrides" do
103
- name = :user
104
- attr = 'attribute'
105
- overrides = { :first_name => 'Ben' }
106
- stub(attr).name { name }
107
- mock(FactoryGirl::Attribute::Association).new(name, name, overrides) { attr }
108
- subject.association(name, overrides)
109
- factory.attributes.should include(attr)
156
+ context "when a block and value are passed" do
157
+ it "raises an exception" do
158
+ expect do
159
+ subject.add_attribute(attribute_name, attribute_value) { "block" }
160
+ end.to raise_error(FactoryGirl::AttributeDefinitionError, "Both value and block given")
161
+ end
110
162
  end
163
+ end
111
164
 
112
- it "should add an attribute using the method name when passed an undefined method" do
113
- attribute = 'attribute'
114
- stub(attribute).name { :name }
115
- mock(FactoryGirl::Attribute::Static).new(:name, 'value') { attribute }
116
- subject.send(:name, 'value')
117
- factory.attributes.should include(attribute)
165
+ describe FactoryGirl::DefinitionProxy, "#association" do
166
+ let(:factory) { FactoryGirl::Factory.new(:object) }
167
+ subject { FactoryGirl::DefinitionProxy.new(factory) }
168
+ let(:attribute) { stub("attribute") }
169
+ let(:association_name) { :author }
170
+ let(:factory_name) { :user }
171
+
172
+ before do
173
+ FactoryGirl::Attribute::Association.stubs(:new => attribute)
174
+ factory.stubs(:define_attribute)
118
175
  end
119
176
 
120
- it "adds an attribute using when passed an undefined method and block" do
121
- attribute = 'attribute'
122
- stub(attribute).name { :name }
123
- block = lambda {}
124
- mock(FactoryGirl::Attribute::Dynamic).new(:name, block) { attribute }
125
- subject.send(:name, &block)
126
- factory.attributes.should include(attribute)
177
+ context "with a factory set in the hash" do
178
+ let(:options) { { :factory => factory_name, :name => "John Doe" } }
179
+
180
+ it "defines an association attribute with the factory name" do
181
+ subject.association(association_name, options)
182
+
183
+ factory.should have_received(:define_attribute).with(attribute)
184
+ FactoryGirl::Attribute::Association.should have_received(:new).with(association_name, factory_name, :name => "John Doe")
185
+ end
186
+
187
+ it "defines an association attribute when the association is called implicitly" do
188
+ subject.send(association_name, options)
189
+
190
+ factory.should have_received(:define_attribute).with(attribute)
191
+ FactoryGirl::Attribute::Association.should have_received(:new).with(association_name, factory_name, :name => "John Doe")
192
+ end
127
193
  end
128
194
 
129
- it "adds an implicit attribute when passed an undefined method without arguments or a block" do
130
- name = :user
131
- attr = 'attribute'
132
- stub(attr).name { name }
133
- mock(FactoryGirl::Attribute::Implicit).new(name, factory) { attr }
134
- subject.send(name)
135
- factory.attributes.should include(attr)
136
- end
137
-
138
- it "adds an association when passed an undefined method with a hash including :factory key" do
139
- name = :author
140
- factory_name = :user
141
- overrides = { :first_name => 'Ben' }
142
- args = { :factory => factory_name }.merge(overrides)
143
- attr = 'attribute'
144
- stub(attr).name { name }
145
- mock(FactoryGirl::Attribute::Association).new(name, factory_name, overrides) { attr }
146
- subject.send(name, args)
147
- factory.attributes.should include(attr)
148
- end
149
-
150
- it "delegates to_create" do
151
- result = 'expected'
152
- mock(factory).to_create { result }
153
-
154
- subject.to_create.should == result
195
+ context "without a factory set in the hash" do
196
+ let(:options) { { :name => "Jane Doe" } }
197
+
198
+ it "defines an association attribute with the association name" do
199
+ subject.association(association_name, options)
200
+
201
+ factory.should have_received(:define_attribute).with(attribute)
202
+ FactoryGirl::Attribute::Association.should have_received(:new).with(association_name, association_name, options)
203
+ end
155
204
  end
156
205
  end
@@ -1,66 +1,45 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe "accessing an undefined method on Factory that is defined on FactoryGirl" do
4
- let(:method_name) { :aliases }
4
+ let(:method_name) { :aliases }
5
5
  let(:return_value) { 'value' }
6
- let(:args) { [1, 2, 3] }
6
+ let(:args) { [1, 2, 3] }
7
7
 
8
8
  before do
9
- stub($stderr).puts
10
- stub(FactoryGirl, method_name).returns { return_value }
11
-
12
- @result = Factory.send(method_name, *args)
9
+ FactoryGirl.stubs(method_name => return_value)
13
10
  end
14
11
 
12
+ subject { Factory.send(method_name, *args) }
13
+
15
14
  it "prints a deprecation warning" do
16
- $stderr.should have_received.puts(anything)
15
+ $stderr.stubs(:puts)
16
+ subject
17
+ $stderr.should have_received(:puts).with(anything)
17
18
  end
18
19
 
19
20
  it "invokes that method on FactoryGirl" do
20
- FactoryGirl.should have_received.method_missing(method_name, *args)
21
+ subject
22
+ FactoryGirl.should have_received(method_name).with(*args)
21
23
  end
22
24
 
23
25
  it "returns the value from the method on FactoryGirl" do
24
- @result.should == return_value
26
+ subject.should == return_value
25
27
  end
26
28
  end
27
29
 
28
30
  describe "accessing an undefined method on Factory that is not defined on FactoryGirl" do
29
- let(:method_name) { :magic_beans }
30
-
31
- before do
32
- stub($stderr).puts { raise "Don't print a deprecation warning" }
33
-
34
- begin
35
- Factory.send(method_name)
36
- rescue Exception => @raised
37
- end
38
- end
39
-
40
31
  it "raises a NoMethodError" do
41
- @raised.should be_a(NoMethodError)
32
+ expect { Factory.send(:magic_beans) }.to raise_error(NoMethodError)
42
33
  end
43
34
  end
44
35
 
45
36
  describe "accessing an undefined constant on Factory that is defined on FactoryGirl" do
46
- before do
47
- @result = Factory::VERSION
48
- end
49
-
50
- it "returns that constant on FactoryGirl" do
51
- @result.should == FactoryGirl::VERSION
52
- end
37
+ subject { Factory::VERSION }
38
+ it { should == FactoryGirl::VERSION }
53
39
  end
54
40
 
55
41
  describe "accessing an undefined constant on Factory that is undefined on FactoryGirl" do
56
42
  it "raises a NameError for Factory" do
57
- begin
58
- Factory::BOGUS
59
- rescue Exception => exception
60
- end
61
-
62
- exception.should be_a(NameError)
63
- exception.message.should include("Factory::BOGUS")
43
+ expect { Factory::BOGUS }.to raise_error(NameError, /Factory::BOGUS/)
64
44
  end
65
45
  end
66
-
@@ -1,8 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe FactoryGirl::Factory do
4
- include DefinesConstants
5
-
6
4
  before do
7
5
  @name = :user
8
6
  @class = define_class('User')
@@ -25,48 +23,16 @@ describe FactoryGirl::Factory do
25
23
  @factory.default_strategy.should == :create
26
24
  end
27
25
 
28
- describe "after adding an attribute" do
29
- before do
30
- @attribute = "attribute"
31
- @proxy = "proxy"
32
-
33
- stub(@attribute).name { :name }
34
- stub(@attribute).add_to
35
- stub(@proxy).set
36
- stub(@proxy).result { 'result' }
37
- stub(FactoryGirl::Attribute::Static).new { @attribute }
38
- stub(FactoryGirl::Proxy::Build).new { @proxy }
39
-
40
- @factory.define_attribute(@attribute)
41
- end
42
-
43
- it "should create the right proxy using the build class when running" do
44
- mock(FactoryGirl::Proxy::Build).new(@factory.build_class) { @proxy }
45
- @factory.run(FactoryGirl::Proxy::Build, {})
46
- end
47
-
48
- it "should add the attribute to the proxy when running" do
49
- mock(@attribute).add_to(@proxy)
50
- @factory.run(FactoryGirl::Proxy::Build, {})
51
- end
52
-
53
- it "should return the result from the proxy when running" do
54
- mock(@proxy).result(nil) { 'result' }
55
- @factory.run(FactoryGirl::Proxy::Build, {}).should == 'result'
56
- end
57
- end
58
-
59
26
  it "passes a custom creation block" do
60
- proxy = 'proxy'
61
- stub(FactoryGirl::Proxy::Build).new { proxy }
62
- stub(proxy).result {}
27
+ proxy = stub("proxy", :result => nil)
28
+ FactoryGirl::Proxy::Build.stubs(:new => proxy)
63
29
  block = lambda {}
64
30
  factory = FactoryGirl::Factory.new(:object)
65
31
  factory.to_create(&block)
66
32
 
67
33
  factory.run(FactoryGirl::Proxy::Build, {})
68
34
 
69
- proxy.should have_received.result(block)
35
+ proxy.should have_received(:result).with(block)
70
36
  end
71
37
 
72
38
  it "should return associations" do
@@ -202,169 +168,174 @@ describe FactoryGirl::Factory do
202
168
  end
203
169
 
204
170
  describe FactoryGirl::Factory, "when defined with a custom class" do
205
- before do
206
- @class = Float
207
- @factory = FactoryGirl::Factory.new(:author, :class => @class)
208
- end
209
-
210
- it "should use the specified class as the build class" do
211
- @factory.build_class.should == @class
212
- end
171
+ subject { FactoryGirl::Factory.new(:author, :class => Float) }
172
+ its(:build_class) { should == Float }
213
173
  end
214
174
 
215
175
  describe FactoryGirl::Factory, "when defined with a class instead of a name" do
216
- before do
217
- @class = ArgumentError
218
- @name = :argument_error
219
- @factory = FactoryGirl::Factory.new(@class)
220
- end
176
+ let(:factory_class) { ArgumentError }
177
+ let(:name) { :argument_error }
221
178
 
222
- it "should guess the name from the class" do
223
- @factory.name.should == @name
224
- end
179
+ subject { FactoryGirl::Factory.new(factory_class) }
225
180
 
226
- it "should use the class as the build class" do
227
- @factory.build_class.should == @class
228
- end
181
+ its(:name) { should == name }
182
+ its(:build_class) { should == factory_class }
229
183
  end
230
184
 
231
185
  describe FactoryGirl::Factory, "when defined with a custom class name" do
232
- before do
233
- @class = ArgumentError
234
- @factory = FactoryGirl::Factory.new(:author, :class => :argument_error)
235
- end
236
-
237
- it "should use the specified class as the build class" do
238
- @factory.build_class.should == @class
239
- end
186
+ subject { FactoryGirl::Factory.new(:author, :class => :argument_error) }
187
+ its(:build_class) { should == ArgumentError }
240
188
  end
241
189
 
242
190
  describe FactoryGirl::Factory, "with a name ending in s" do
243
- include DefinesConstants
244
-
245
- before do
246
- define_class('Business')
247
- @name = :business
248
- @class = Business
249
- @factory = FactoryGirl::Factory.new(@name)
250
- end
191
+ let(:name) { :business }
192
+ let(:business_class) { Business }
251
193
 
252
- it "should have a factory name" do
253
- @factory.name.should == @name
254
- end
194
+ before { define_class('Business') }
195
+ subject { FactoryGirl::Factory.new(name) }
255
196
 
256
- it "should have a build class" do
257
- @factory.build_class.should == @class
258
- end
197
+ its(:name) { should == name }
198
+ its(:build_class) { should == business_class }
259
199
  end
260
200
 
261
201
  describe FactoryGirl::Factory, "with a string for a name" do
262
- before do
263
- @name = :string
264
- @factory = FactoryGirl::Factory.new(@name.to_s) {}
265
- end
266
-
267
- it "should convert the string to a symbol" do
268
- @factory.name.should == @name
269
- end
202
+ let(:name) { :string }
203
+ subject { FactoryGirl::Factory.new(name.to_s) }
204
+ its(:name) { should == name }
270
205
  end
271
206
 
272
207
  describe FactoryGirl::Factory, "for namespaced class" do
273
- include DefinesConstants
208
+ let(:name) { :settings }
209
+ let(:settings_class) { Admin::Settings }
274
210
 
275
211
  before do
276
- define_class('Admin')
277
- define_class('Admin::Settings')
278
-
279
- @name = :settings
280
- @class = Admin::Settings
212
+ define_class("Admin")
213
+ define_class("Admin::Settings")
281
214
  end
282
215
 
283
- it "should build namespaced class passed by string" do
284
- factory = FactoryGirl::Factory.new(@name.to_s, :class => @class.name)
285
- factory.build_class.should == @class
216
+ context "with a namespaced class with Namespace::Class syntax" do
217
+ subject { FactoryGirl::Factory.new(name, :class => "Admin::Settings") }
218
+
219
+ it "sets build_class correctly" do
220
+ subject.build_class.should == settings_class
221
+ end
286
222
  end
287
223
 
288
- it "should build Admin::Settings class from Admin::Settings string" do
289
- factory = FactoryGirl::Factory.new(@name.to_s, :class => 'admin/settings')
290
- factory.build_class.should == @class
224
+ context "with a namespaced class with namespace/class syntax" do
225
+ subject { FactoryGirl::Factory.new(name, :class => "admin/settings") }
226
+
227
+ it "sets build_class correctly" do
228
+ subject.build_class.should == settings_class
229
+ end
291
230
  end
292
231
  end
293
232
 
294
233
  describe FactoryGirl::Factory do
295
- include DefinesConstants
234
+ let(:factory_with_non_existent_strategy) do
235
+ FactoryGirl::Factory.new(:object, :default_strategy => :nonexistent) { }
236
+ end
237
+
238
+ let(:factory_with_stub_strategy) do
239
+ FactoryGirl::Factory.new(:object, :default_strategy => :stub)
240
+ end
241
+
242
+ let(:factory_without_strategy) do
243
+ FactoryGirl::Factory.new(:other_object)
244
+ end
245
+
246
+ let(:factory_with_build_strategy) do
247
+ FactoryGirl::Factory.new(:other_object, :default_strategy => :build)
248
+ end
296
249
 
297
250
  before do
298
- define_class('User')
299
- define_class('Admin', User)
251
+ define_class("User")
252
+ define_class("Admin", User)
300
253
  end
301
254
 
302
- it "should raise an ArgumentError when trying to use a non-existent strategy" do
303
- lambda {
304
- FactoryGirl::Factory.new(:object, :default_strategy => :nonexistent) {}
305
- }.should raise_error(ArgumentError)
255
+ it "raises an ArgumentError when trying to use a non-existent strategy" do
256
+ expect { factory_with_non_existent_strategy }.to raise_error(ArgumentError)
306
257
  end
307
258
 
308
- it "should create a new factory with a specified default strategy" do
309
- factory = FactoryGirl::Factory.new(:object, :default_strategy => :stub)
310
- factory.default_strategy.should == :stub
259
+ it "creates a new factory with a specified default strategy" do
260
+ factory_with_stub_strategy.default_strategy.should == :stub
311
261
  end
312
262
 
313
- describe 'defining a child factory without setting default strategy' do
263
+ describe "defining a child factory without setting default strategy" do
264
+ let(:parent) { factory_with_stub_strategy }
265
+ subject { factory_without_strategy }
266
+
314
267
  before do
315
- @parent = FactoryGirl::Factory.new(:object, :default_strategy => :stub)
316
- @child = FactoryGirl::Factory.new(:child_object)
317
- @child.inherit_from(@parent)
268
+ subject.inherit_from(parent)
318
269
  end
319
270
 
320
- it "should inherit default strategy from its parent" do
321
- @child.default_strategy.should == :stub
271
+ it "inherits default strategy from its parent" do
272
+ subject.default_strategy.should == :stub
322
273
  end
323
274
  end
324
275
 
325
- describe 'defining a child factory with a default strategy' do
276
+ describe "defining a child factory with a default strategy" do
277
+ let(:parent) { factory_with_stub_strategy }
278
+ subject { factory_with_build_strategy }
279
+
326
280
  before do
327
- @parent = FactoryGirl::Factory.new(:object, :default_strategy => :stub)
328
- @child = FactoryGirl::Factory.new(:child_object2, :default_strategy => :build)
329
- @child.inherit_from(@parent)
281
+ subject.inherit_from(parent)
330
282
  end
331
283
 
332
- it "should override the default strategy from parent" do
333
- @child.default_strategy.should == :build
284
+ it "overrides the default strategy from parent" do
285
+ subject.default_strategy.should == :build
334
286
  end
335
287
  end
336
-
337
288
  end
338
289
 
339
290
  describe FactoryGirl::Factory, "human names" do
340
291
  context "factory name without underscores" do
341
- subject { FactoryGirl::Factory.new("user") }
292
+ subject { FactoryGirl::Factory.new(:user) }
293
+ its(:names) { should == [:user] }
342
294
  its(:human_names) { should == ["user"] }
343
295
  end
344
296
 
345
297
  context "factory name with underscores" do
346
- subject { FactoryGirl::Factory.new("happy_user") }
298
+ subject { FactoryGirl::Factory.new(:happy_user) }
299
+ its(:names) { should == [:happy_user] }
347
300
  its(:human_names) { should == ["happy user"] }
348
301
  end
349
302
 
350
303
  context "factory name with aliases" do
351
- subject { FactoryGirl::Factory.new("happy_user", :aliases => ["gleeful_user", "person"]) }
304
+ subject { FactoryGirl::Factory.new(:happy_user, :aliases => [:gleeful_user, :person]) }
305
+ its(:names) { should == [:happy_user, :gleeful_user, :person] }
352
306
  its(:human_names) { should == ["happy user", "gleeful user", "person"] }
353
307
  end
354
308
  end
355
309
 
356
- describe FactoryGirl::Factory, "with aliases" do
357
- it "registers the aliases" do
358
- name = :user
359
- aliased_name = :guest
360
- factory = FactoryGirl::Factory.new(:user, :aliases => [aliased_name])
361
- factory.names.should =~ [name, aliased_name]
310
+ describe FactoryGirl::Factory, "running a factory" do
311
+ subject { FactoryGirl::Factory.new(:user) }
312
+ let(:attribute) { stub("attribute", :name => :name, :ignored => false, :add_to => nil, :aliases_for? => true) }
313
+ let(:proxy) { stub("proxy", :result => "result", :set => nil) }
314
+
315
+ before do
316
+ define_model("User", :name => :string)
317
+ FactoryGirl::Attribute::Static.stubs(:new => attribute)
318
+ FactoryGirl::Proxy::Build.stubs(:new => proxy)
319
+ FactoryGirl::AttributeList.stubs(:new => [attribute])
320
+ end
321
+
322
+ it "creates the right proxy using the build class when running" do
323
+ subject.run(FactoryGirl::Proxy::Build, {})
324
+ FactoryGirl::Proxy::Build.should have_received(:new).with(subject.build_class)
325
+ end
326
+
327
+ it "adds the attribute to the proxy when running" do
328
+ subject.run(FactoryGirl::Proxy::Build, {})
329
+ attribute.should have_received(:add_to).with(proxy)
330
+ end
331
+
332
+ it "returns the result from the proxy when running" do
333
+ subject.run(FactoryGirl::Proxy::Build, {}).should == "result"
334
+ proxy.should have_received(:result).with(nil)
362
335
  end
363
336
 
364
- it "has human names" do
365
- name = :user
366
- aliased_name = :guest
367
- factory = FactoryGirl::Factory.new(:user, :aliases => [aliased_name])
368
- factory.human_names.should =~ [name.to_s, aliased_name.to_s]
337
+ it "sets overrides once on the factory" do
338
+ subject.run(FactoryGirl::Proxy::Build, { :name => "John Doe" })
339
+ proxy.should have_received(:set).once
369
340
  end
370
341
  end