factory_girl 2.2.0 → 2.3.0

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 (58) hide show
  1. data/.autotest +1 -1
  2. data/CONTRIBUTION_GUIDELINES.md +1 -1
  3. data/Changelog +16 -3
  4. data/GETTING_STARTED.md +22 -2
  5. data/Gemfile.lock +1 -1
  6. data/Rakefile +11 -15
  7. data/gemfiles/2.1.gemfile.lock +1 -1
  8. data/gemfiles/2.3.gemfile.lock +1 -1
  9. data/gemfiles/3.0.gemfile.lock +1 -1
  10. data/gemfiles/3.1.gemfile.lock +1 -1
  11. data/lib/factory_girl.rb +8 -3
  12. data/lib/factory_girl/attribute.rb +8 -0
  13. data/lib/factory_girl/attribute/dynamic.rb +1 -5
  14. data/lib/factory_girl/attribute/sequence.rb +1 -5
  15. data/lib/factory_girl/attribute/static.rb +1 -5
  16. data/lib/factory_girl/attribute_list.rb +18 -44
  17. data/lib/factory_girl/callback.rb +5 -0
  18. data/lib/factory_girl/declaration.rb +3 -0
  19. data/lib/factory_girl/declaration/association.rb +8 -0
  20. data/lib/factory_girl/declaration/dynamic.rb +9 -0
  21. data/lib/factory_girl/declaration/implicit.rb +11 -2
  22. data/lib/factory_girl/declaration/static.rb +9 -0
  23. data/lib/factory_girl/declaration_list.rb +48 -0
  24. data/lib/factory_girl/definition.rb +62 -0
  25. data/lib/factory_girl/definition_proxy.rb +11 -11
  26. data/lib/factory_girl/factory.rb +100 -111
  27. data/lib/factory_girl/null_factory.rb +15 -0
  28. data/lib/factory_girl/proxy.rb +14 -9
  29. data/lib/factory_girl/proxy/attributes_for.rb +2 -3
  30. data/lib/factory_girl/proxy/build.rb +12 -20
  31. data/lib/factory_girl/proxy/create.rb +0 -6
  32. data/lib/factory_girl/proxy/stub.rb +4 -10
  33. data/lib/factory_girl/registry.rb +4 -3
  34. data/lib/factory_girl/step_definitions.rb +1 -1
  35. data/lib/factory_girl/syntax/default.rb +3 -4
  36. data/lib/factory_girl/syntax/methods.rb +38 -16
  37. data/lib/factory_girl/trait.rb +13 -21
  38. data/lib/factory_girl/version.rb +1 -1
  39. data/spec/acceptance/modify_factories_spec.rb +1 -1
  40. data/spec/acceptance/traits_spec.rb +87 -1
  41. data/spec/factory_girl/attribute/dynamic_spec.rb +1 -1
  42. data/spec/factory_girl/attribute_list_spec.rb +9 -58
  43. data/spec/factory_girl/declaration_list_spec.rb +71 -0
  44. data/spec/factory_girl/definition_proxy_spec.rb +135 -139
  45. data/spec/factory_girl/definition_spec.rb +81 -0
  46. data/spec/factory_girl/factory_spec.rb +42 -17
  47. data/spec/factory_girl/null_factory_spec.rb +12 -0
  48. data/spec/factory_girl/proxy/build_spec.rb +1 -24
  49. data/spec/factory_girl/proxy/create_spec.rb +14 -11
  50. data/spec/factory_girl/proxy_spec.rb +23 -40
  51. data/spec/factory_girl/registry_spec.rb +4 -3
  52. data/spec/spec_helper.rb +2 -0
  53. data/spec/support/matchers/callback.rb +9 -0
  54. data/spec/support/matchers/declaration.rb +71 -0
  55. data/spec/support/matchers/delegate.rb +44 -0
  56. data/spec/support/matchers/trait.rb +9 -0
  57. data/spec/support/shared_examples/proxy.rb +4 -5
  58. metadata +191 -115
@@ -51,6 +51,6 @@ describe FactoryGirl::Attribute::Dynamic do
51
51
  end
52
52
 
53
53
  describe FactoryGirl::Attribute::Dynamic, "with a string name" do
54
- subject { FactoryGirl::Attribute::Dynamic.new("name", nil, false) }
54
+ subject { FactoryGirl::Attribute::Dynamic.new("name", false, lambda { } ) }
55
55
  its(:name) { should == :name }
56
56
  end
@@ -1,14 +1,5 @@
1
1
  require "spec_helper"
2
2
 
3
- describe FactoryGirl::AttributeList, "overridable" do
4
- it { should_not be_overridable }
5
-
6
- it "can set itself as overridable" do
7
- subject.overridable
8
- subject.should be_overridable
9
- end
10
- end
11
-
12
3
  describe FactoryGirl::AttributeList, "#define_attribute" do
13
4
  let(:static_attribute) { FactoryGirl::Attribute::Static.new(:full_name, "value", false) }
14
5
  let(:dynamic_attribute) { FactoryGirl::Attribute::Dynamic.new(:email, false, lambda {|u| "#{u.full_name}@example.com" }) }
@@ -31,29 +22,20 @@ describe FactoryGirl::AttributeList, "#define_attribute" do
31
22
  2.times { subject.define_attribute(static_attribute) }
32
23
  }.to raise_error(FactoryGirl::AttributeDefinitionError, "Attribute already defined: full_name")
33
24
  end
25
+ end
34
26
 
35
- context "when set as overridable" do
36
- let(:static_attribute_with_same_name) { FactoryGirl::Attribute::Static.new(:full_name, "overridden value", false) }
37
- before { subject.overridable }
27
+ describe FactoryGirl::AttributeList, "#define_attribute with a named attribute list" do
28
+ subject { FactoryGirl::AttributeList.new(:author) }
38
29
 
39
- it "redefines the attribute if the name already exists" do
40
- subject.define_attribute(static_attribute)
41
- subject.define_attribute(static_attribute_with_same_name)
30
+ let(:association_with_same_name) { FactoryGirl::Attribute::Association.new(:author, :author, {}) }
31
+ let(:association_with_different_name) { FactoryGirl::Attribute::Association.new(:author, :post, {}) }
42
32
 
43
- subject.to_a.should == [static_attribute_with_same_name]
44
- end
33
+ it "raises when the attribute is a self-referencing association" do
34
+ expect { subject.define_attribute(association_with_same_name) }.to raise_error(FactoryGirl::AssociationDefinitionError, "Self-referencing association 'author' in 'author'")
45
35
  end
46
- end
47
-
48
- describe FactoryGirl::AttributeList, "#add_callback" do
49
- let(:proxy_class) { mock("klass") }
50
- let(:proxy) { FactoryGirl::Proxy.new(proxy_class) }
51
36
 
52
- it "allows for defining adding a callback" do
53
- subject.add_callback(FactoryGirl::Callback.new(:after_create, lambda { "Called after_create" }))
54
-
55
- subject.callbacks.first.name.should == :after_create
56
- subject.callbacks.first.run(nil, nil).should == "Called after_create"
37
+ it "does not raise when the attribute is not a self-referencing association" do
38
+ expect { subject.define_attribute(association_with_different_name) }.to_not raise_error
57
39
  end
58
40
  end
59
41
 
@@ -95,35 +77,4 @@ describe FactoryGirl::AttributeList, "#apply_attributes" do
95
77
  subject.apply_attributes(list(attribute_with_same_name))
96
78
  subject.to_a.should == [full_name_attribute]
97
79
  end
98
-
99
- context "when set as overridable" do
100
- before { subject.overridable }
101
-
102
- it "prepends applied attributes" do
103
- subject.define_attribute(full_name_attribute)
104
- subject.apply_attributes(list(city_attribute))
105
- subject.to_a.should == [city_attribute, full_name_attribute]
106
- end
107
-
108
- it "moves non-static attributes to the end of the list" do
109
- subject.define_attribute(full_name_attribute)
110
- subject.apply_attributes(list(city_attribute, email_attribute))
111
- subject.to_a.should == [city_attribute, full_name_attribute, email_attribute]
112
- end
113
-
114
- it "maintains order of non-static attributes" do
115
- subject.define_attribute(full_name_attribute)
116
- subject.define_attribute(login_attribute)
117
- subject.apply_attributes(list(city_attribute, email_attribute))
118
- subject.to_a.should == [city_attribute, full_name_attribute, email_attribute, login_attribute]
119
- end
120
-
121
- it "overwrites attributes that are already defined" do
122
- subject.define_attribute(full_name_attribute)
123
- attribute_with_same_name = FactoryGirl::Attribute::Static.new(:full_name, "Benjamin Franklin", false)
124
-
125
- subject.apply_attributes(list(attribute_with_same_name))
126
- subject.to_a.should == [attribute_with_same_name]
127
- end
128
- end
129
80
  end
@@ -0,0 +1,71 @@
1
+ require "spec_helper"
2
+
3
+ describe FactoryGirl::DeclarationList, "#attribute_list" do
4
+ let(:static_attribute_1) { stub("static attribute 1") }
5
+ let(:static_attribute_2) { stub("static attribute 2") }
6
+ let(:dynamic_attribute_1) { stub("dynamic attribute 1") }
7
+ let(:static_declaration) { stub("static declaration", :to_attributes => [static_attribute_1, static_attribute_2]) }
8
+ let(:dynamic_declaration) { stub("static declaration", :to_attributes => [dynamic_attribute_1]) }
9
+
10
+ it "returns an AttributeList" do
11
+ subject.attribute_list.should be_a(FactoryGirl::AttributeList)
12
+ end
13
+
14
+ let(:attribute_list) { stub("attribute list", :define_attribute => true) }
15
+
16
+ it "defines each attribute on the attribute list" do
17
+ FactoryGirl::AttributeList.stubs(:new => attribute_list)
18
+
19
+ subject.declare_attribute(static_declaration)
20
+ subject.declare_attribute(dynamic_declaration)
21
+
22
+ subject.attribute_list
23
+
24
+ attribute_list.should have_received(:define_attribute).with(static_attribute_1)
25
+ attribute_list.should have_received(:define_attribute).with(static_attribute_2)
26
+ attribute_list.should have_received(:define_attribute).with(dynamic_attribute_1)
27
+ end
28
+
29
+ it "creates a new attribute list upon every invocation" do
30
+ subject.attribute_list.should_not == subject.attribute_list
31
+ end
32
+ end
33
+
34
+ describe FactoryGirl::DeclarationList, "#declare_attribute" do
35
+ let(:declaration_1) { stub("declaration", :name => "declaration 1") }
36
+ let(:declaration_2) { stub("declaration", :name => "declaration 2") }
37
+ let(:declaration_with_same_name) { stub("declaration", :name => "declaration 1") }
38
+
39
+ context "when not overridable" do
40
+ it "adds the declaration to the list" do
41
+ subject.declare_attribute(declaration_1)
42
+ subject.to_a.should == [declaration_1]
43
+
44
+ subject.declare_attribute(declaration_2)
45
+ subject.to_a.should == [declaration_1, declaration_2]
46
+ end
47
+ end
48
+
49
+ context "when overridable" do
50
+ before { subject.overridable }
51
+
52
+ it "adds the declaration to the list" do
53
+ subject.declare_attribute(declaration_1)
54
+ subject.to_a.should == [declaration_1]
55
+
56
+ subject.declare_attribute(declaration_2)
57
+ subject.to_a.should == [declaration_1, declaration_2]
58
+ end
59
+
60
+ it "deletes declarations with the same name" do
61
+ subject.declare_attribute(declaration_1)
62
+ subject.to_a.should == [declaration_1]
63
+
64
+ subject.declare_attribute(declaration_2)
65
+ subject.to_a.should == [declaration_1, declaration_2]
66
+
67
+ subject.declare_attribute(declaration_with_same_name)
68
+ subject.to_a.should == [declaration_2, declaration_with_same_name]
69
+ end
70
+ end
71
+ end
@@ -1,190 +1,186 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe FactoryGirl::DefinitionProxy do
4
- let(:factory) { FactoryGirl::Factory.new(:object) }
5
- subject { FactoryGirl::DefinitionProxy.new(factory) }
6
-
7
- it "adds a static attribute when an attribute is defined with a value" do
8
- attribute = stub('attribute', :name => :name)
9
- FactoryGirl::Attribute::Static.stubs(:new => attribute)
10
- factory.stubs(:define_attribute)
11
- subject.add_attribute(:name, 'value')
12
- factory.ensure_compiled
13
- factory.should have_received(:define_attribute).with(attribute)
14
- FactoryGirl::Attribute::Static.should have_received(:new).with(:name, "value", false)
15
- end
16
-
17
- it "adds a dynamic attribute when an attribute is defined with a block" do
18
- attribute = stub('attribute', :name => :name)
19
- block = lambda {}
20
- FactoryGirl::Attribute::Dynamic.stubs(:new => attribute)
21
- factory.stubs(:define_attribute)
22
- subject.add_attribute(:name, &block)
23
- factory.ensure_compiled
24
- FactoryGirl::Attribute::Dynamic.should have_received(:new).with(:name, false, block)
25
- factory.should have_received(:define_attribute).with(attribute)
26
- end
27
-
28
- it "raises for an attribute with a value and a block" do
3
+ describe FactoryGirl::DefinitionProxy, "#add_attribute" do
4
+ subject { FactoryGirl::Definition.new }
5
+ let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) }
6
+
7
+ it "raises if both a block and value are given" do
29
8
  expect {
30
- subject.add_attribute(:name, 'value') {}
31
- }.to raise_error(FactoryGirl::AttributeDefinitionError)
9
+ proxy.add_attribute(:something, "great") { "will raise!" }
10
+ }.to raise_error(FactoryGirl::AttributeDefinitionError, "Both value and block given")
32
11
  end
33
12
 
34
- describe "child factories" do
35
- its(:child_factories) { should == [] }
13
+ it "declares a static attribute on the factory" do
14
+ proxy.add_attribute(:attribute_name, "attribute value")
15
+ subject.should have_static_declaration(:attribute_name).with_value("attribute value")
16
+ end
36
17
 
37
- it "is be able to add child factories" do
38
- block = lambda {}
39
- subject.factory(:admin, { :aliases => [:great] }, &block)
40
- subject.child_factories.should == [[:admin, { :aliases => [:great] }, block]]
41
- end
18
+ it "declares a dynamic attribute on the factory" do
19
+ attribute_value = lambda { "dynamic attribute" }
20
+ proxy.add_attribute(:attribute_name, &attribute_value)
21
+ subject.should have_dynamic_declaration(:attribute_name).with_value(attribute_value)
42
22
  end
23
+ end
43
24
 
44
- describe "adding an attribute using a in-line sequence" do
45
- it "creates the sequence" do
46
- FactoryGirl::Sequence.stubs(:new)
47
- subject.sequence(:name) {}
48
- FactoryGirl::Sequence.should have_received(:new).with(:name, 1)
49
- end
25
+ describe FactoryGirl::DefinitionProxy, "#add_attribute when the proxy ignores attributes" do
26
+ subject { FactoryGirl::Definition.new }
27
+ let(:proxy) { FactoryGirl::DefinitionProxy.new(subject, true) }
50
28
 
51
- it "creates the sequence with a custom default value" do
52
- FactoryGirl::Sequence.stubs(:new)
53
- subject.sequence(:name, "A") {}
54
- FactoryGirl::Sequence.should have_received(:new).with(:name, "A")
55
- end
29
+ it "raises if both a block and value are given" do
30
+ expect {
31
+ proxy.add_attribute(:something, "great") { "will raise!" }
32
+ }.to raise_error(FactoryGirl::AttributeDefinitionError, "Both value and block given")
33
+ end
34
+
35
+ it "declares a static attribute on the factory" do
36
+ proxy.add_attribute(:attribute_name, "attribute value")
37
+ subject.should have_static_declaration(:attribute_name).ignored.with_value("attribute value")
38
+ end
39
+
40
+ it "declares a dynamic attribute on the factory" do
41
+ attribute_value = lambda { "dynamic attribute" }
42
+ proxy.add_attribute(:attribute_name, &attribute_value)
43
+ subject.should have_dynamic_declaration(:attribute_name).ignored.with_value(attribute_value)
56
44
  end
57
45
  end
58
46
 
59
- describe FactoryGirl::DefinitionProxy, "with a factory mock" do
60
- before do
61
- define_class("FactoryMock") do
62
- def add_callback(callback, &block)
63
- [callback, block.call]
64
- end
47
+ describe FactoryGirl::DefinitionProxy, "#ignore" do
48
+ subject { FactoryGirl::Definition.new }
49
+ let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) }
65
50
 
66
- def to_create(&block)
67
- block.call
68
- end
51
+ it "makes all attributes added ignored" do
52
+ proxy.ignore do
53
+ add_attribute(:attribute_name, "attribute value")
69
54
  end
55
+
56
+ subject.should have_static_declaration(:attribute_name).ignored.with_value("attribute value")
70
57
  end
58
+ end
71
59
 
72
- let(:factory_mock) { FactoryMock.new }
73
- subject { FactoryGirl::DefinitionProxy.new(factory_mock) }
60
+ describe FactoryGirl::DefinitionProxy, "#method_missing" do
61
+ subject { FactoryGirl::Definition.new }
62
+ let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) }
74
63
 
75
- it "defines after_build callbacks" do
76
- subject.after_build { "after_build value" }.should == [:after_build, "after_build value"]
64
+ it "declares an implicit declaration without args or a block" do
65
+ proxy.bogus
66
+ subject.should have_implicit_declaration(:bogus).with_factory(subject)
77
67
  end
78
68
 
79
- it "defines after_create callbacks" do
80
- subject.after_create { "after_create value" }.should == [:after_create, "after_create value"]
69
+ it "declares an association when :factory is passed" do
70
+ proxy.author :factory => :user
71
+ subject.should have_association_declaration(:author).with_options(:factory => :user)
81
72
  end
82
73
 
83
- it "defines after_stub callbacks" do
84
- subject.after_stub { "after_stub value" }.should == [:after_stub, "after_stub value"]
74
+ it "declares a static attribute" do
75
+ proxy.attribute_name "attribute value"
76
+ subject.should have_static_declaration(:attribute_name).with_value("attribute value")
85
77
  end
86
78
 
87
- it "defines to_create" do
88
- subject.to_create { "to_create value" }.should == "to_create value"
79
+ it "declares a dynamic attribute" do
80
+ attribute_value = lambda { "dynamic attribute" }
81
+ proxy.attribute_name &attribute_value
82
+ subject.should have_dynamic_declaration(:attribute_name).with_value(attribute_value)
89
83
  end
90
84
  end
91
85
 
92
- describe FactoryGirl::DefinitionProxy, "adding attributes" do
93
- let(:factory) { FactoryGirl::Factory.new(:object) }
94
- subject { FactoryGirl::DefinitionProxy.new(factory) }
95
- let(:attribute) { stub("created attribute") }
96
- let(:block) { lambda { } }
97
- let(:attribute_name) { :full_name }
98
- let(:attribute_value) { "passed value" }
86
+ describe FactoryGirl::DefinitionProxy, "#sequence" do
87
+ subject { FactoryGirl::Definition.new }
88
+ let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) }
99
89
 
100
- before { factory.stubs(:define_attribute) }
90
+ before { FactoryGirl::Sequence.stubs(:new) }
101
91
 
102
- context "when a block is passed" do
103
- before { FactoryGirl::Attribute::Dynamic.stubs(:new => attribute) }
104
-
105
- it "creates a dynamic attribute" do
106
- subject.add_attribute(attribute_name, &block)
107
- factory.ensure_compiled
108
- FactoryGirl::Attribute::Dynamic.should have_received(:new).with(attribute_name, false, block)
109
- factory.should have_received(:define_attribute).with(attribute)
110
- end
92
+ it "creates a new sequence starting at 1" do
93
+ proxy.sequence(:great)
94
+ FactoryGirl::Sequence.should have_received(:new).with(:great, 1)
95
+ end
111
96
 
112
- it "creates a dynamic attribute without the method being defined" do
113
- subject.send(attribute_name, &block)
114
- factory.ensure_compiled
115
- FactoryGirl::Attribute::Dynamic.should have_received(:new).with(attribute_name, false, block)
116
- factory.should have_received(:define_attribute).with(attribute)
117
- end
97
+ it "creates a new sequence with an overridden starting vaue" do
98
+ proxy.sequence(:great, "C")
99
+ FactoryGirl::Sequence.should have_received(:new).with(:great, "C")
118
100
  end
119
101
 
120
- context "when a value is passed" do
121
- before { FactoryGirl::Attribute::Static.stubs(:new => attribute) }
102
+ it "creates a new sequence with a block" do
103
+ sequence_block = Proc.new {|n| "user+#{n}@example.com" }
104
+ proxy.sequence(:great, 1, &sequence_block)
105
+ FactoryGirl::Sequence.should have_received(:new).with(:great, 1, &sequence_block)
106
+ end
107
+ end
122
108
 
123
- it "creates a static attribute" do
124
- subject.add_attribute(attribute_name, attribute_value)
125
- factory.ensure_compiled
126
- FactoryGirl::Attribute::Static.should have_received(:new).with(attribute_name, attribute_value, false)
127
- factory.should have_received(:define_attribute).with(attribute)
128
- end
109
+ describe FactoryGirl::DefinitionProxy, "#association" do
110
+ subject { FactoryGirl::Definition.new }
111
+ let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) }
129
112
 
130
- it "creates a static attribute without the method being defined" do
131
- subject.send(attribute_name, attribute_value)
132
- factory.ensure_compiled
133
- FactoryGirl::Attribute::Static.should have_received(:new).with(attribute_name, attribute_value, false)
134
- factory.should have_received(:define_attribute).with(attribute)
135
- end
113
+ it "declares an association" do
114
+ proxy.association(:association_name)
115
+ subject.should have_association_declaration(:association_name)
136
116
  end
137
117
 
138
- context "when a block and value are passed" do
139
- it "raises an exception" do
140
- expect do
141
- subject.add_attribute(attribute_name, attribute_value) { "block" }
142
- end.to raise_error(FactoryGirl::AttributeDefinitionError, "Both value and block given")
143
- end
118
+ it "declares an association with options" do
119
+ proxy.association(:association_name, { :name => "Awesome" })
120
+ subject.should have_association_declaration(:association_name).with_options(:name => "Awesome")
144
121
  end
145
122
  end
146
123
 
147
- describe FactoryGirl::DefinitionProxy, "#association" do
148
- let(:factory) { FactoryGirl::Factory.new(:object) }
149
- subject { FactoryGirl::DefinitionProxy.new(factory) }
150
- let(:attribute) { stub("attribute") }
151
- let(:association_name) { :author }
152
- let(:factory_name) { :user }
124
+ describe FactoryGirl::DefinitionProxy, "adding callbacks" do
125
+ subject { FactoryGirl::Definition.new }
126
+ let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) }
127
+ let(:callback) { lambda { "my awesome callback!" } }
153
128
 
154
- before do
155
- FactoryGirl::Attribute::Association.stubs(:new => attribute)
156
- factory.stubs(:define_attribute)
129
+ context "#after_build" do
130
+ before { proxy.after_build(&callback) }
131
+ it { should have_callback(:after_build).with_block(callback) }
157
132
  end
158
133
 
159
- context "with a factory set in the hash" do
160
- let(:options) { { :factory => factory_name, :name => "John Doe" } }
134
+ context "#after_create" do
135
+ before { proxy.after_create(&callback) }
136
+ it { should have_callback(:after_create).with_block(callback) }
137
+ end
161
138
 
162
- it "defines an association attribute with the factory name" do
163
- subject.association(association_name, options)
164
- factory.ensure_compiled
139
+ context "#after_stub" do
140
+ before { proxy.after_stub(&callback) }
141
+ it { should have_callback(:after_stub).with_block(callback) }
142
+ end
143
+ end
165
144
 
166
- factory.should have_received(:define_attribute).with(attribute)
167
- FactoryGirl::Attribute::Association.should have_received(:new).with(association_name, factory_name, :name => "John Doe")
168
- end
145
+ describe FactoryGirl::DefinitionProxy, "#to_create" do
146
+ subject { FactoryGirl::Definition.new }
147
+ let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) }
169
148
 
170
- it "defines an association attribute when the association is called implicitly" do
171
- subject.send(association_name, options)
172
- factory.ensure_compiled
149
+ it "accepts a block to run in place of #save!" do
150
+ to_create_block = lambda {|record| record.persist }
151
+ proxy.to_create(&to_create_block)
152
+ subject.to_create.should == to_create_block
153
+ end
154
+ end
173
155
 
174
- factory.should have_received(:define_attribute).with(attribute)
175
- FactoryGirl::Attribute::Association.should have_received(:new).with(association_name, factory_name, :name => "John Doe")
176
- end
156
+ describe FactoryGirl::DefinitionProxy, "#factory" do
157
+ subject { FactoryGirl::Definition.new }
158
+ let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) }
159
+
160
+ it "without options" do
161
+ proxy.factory(:child)
162
+ proxy.child_factories.should include([:child, {}, nil])
177
163
  end
178
164
 
179
- context "without a factory set in the hash" do
180
- let(:options) { { :name => "Jane Doe" } }
165
+ it "with options" do
166
+ proxy.factory(:child, { :awesome => true })
167
+ proxy.child_factories.should include([:child, { :awesome => true }, nil])
168
+ end
181
169
 
182
- it "defines an association attribute with the association name" do
183
- subject.association(association_name, options)
184
- factory.ensure_compiled
170
+ it "with a block" do
171
+ child_block = lambda { }
172
+ proxy.factory(:child, {}, &child_block)
173
+ proxy.child_factories.should include([:child, {}, child_block])
174
+ end
175
+ end
185
176
 
186
- factory.should have_received(:define_attribute).with(attribute)
187
- FactoryGirl::Attribute::Association.should have_received(:new).with(association_name, association_name, options)
188
- end
177
+ describe FactoryGirl::DefinitionProxy, "#trait" do
178
+ subject { FactoryGirl::Definition.new }
179
+ let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) }
180
+
181
+ it "declares a trait" do
182
+ male_trait = Proc.new { gender("Male") }
183
+ proxy.trait(:male, &male_trait)
184
+ subject.should have_trait(:male).with_block(male_trait)
189
185
  end
190
186
  end