gabrielg-factory_girl 1.2.0 → 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/factory_girl/factory.rb +3 -3
- data/test/factory_test.rb +29 -4
- metadata +1 -1
data/lib/factory_girl/factory.rb
CHANGED
@@ -126,8 +126,8 @@ class Factory
|
|
126
126
|
add_attribute(name) { |a| a.association(association_factory, {}, {:count => options[:count]}) }
|
127
127
|
end
|
128
128
|
|
129
|
-
def attributes_for (attrs = {}) #:nodoc:
|
130
|
-
build_attributes_hash(attrs,
|
129
|
+
def attributes_for (attrs = {}, strategy_override = :attributes_for) #:nodoc:
|
130
|
+
build_attributes_hash(attrs, strategy_override)
|
131
131
|
end
|
132
132
|
|
133
133
|
def build (attrs = {}) #:nodoc:
|
@@ -252,7 +252,7 @@ class Factory
|
|
252
252
|
vals[attribute.name] = attribute.value(proxy)
|
253
253
|
vals
|
254
254
|
end
|
255
|
-
inherited_factory ? inherited_factory.attributes_for.merge(attrs) : attrs
|
255
|
+
inherited_factory ? inherited_factory.attributes_for({}, strategy).merge(attrs) : attrs
|
256
256
|
end
|
257
257
|
|
258
258
|
def build_instance (override, strategy)
|
data/test/factory_test.rb
CHANGED
@@ -80,12 +80,16 @@ class FactoryTest < Test::Unit::TestCase
|
|
80
80
|
context "when inheriting from a prior factory" do
|
81
81
|
setup do
|
82
82
|
@factory.foo "bar"
|
83
|
-
@factory.snafu "foo"
|
83
|
+
@factory.snafu { "foo" }
|
84
84
|
@new_factory = Factory.new(:user_with_stuff, :inherit => @factory)
|
85
85
|
@new_factory.foo "fighters"
|
86
86
|
@instance = @new_factory.build
|
87
87
|
end
|
88
88
|
|
89
|
+
teardown do
|
90
|
+
Factory.factories = {}
|
91
|
+
end
|
92
|
+
|
89
93
|
should "use subfactory's class if not specified" do
|
90
94
|
assert_equal User, @factory.build_class
|
91
95
|
end
|
@@ -103,19 +107,36 @@ class FactoryTest < Test::Unit::TestCase
|
|
103
107
|
assert_equal "foo", @instance.snafu
|
104
108
|
end
|
105
109
|
|
110
|
+
should "also inherit associations" do
|
111
|
+
Factory.define(:user) do |u|
|
112
|
+
u.first_name "Joe"
|
113
|
+
u.last_name "Bloggs"
|
114
|
+
u.email "joe@example.com"
|
115
|
+
end
|
116
|
+
|
117
|
+
factory = Factory.new(:post)
|
118
|
+
factory.association(:author, :factory => :user)
|
119
|
+
inherited = Factory.new(:post_with_stuff, :inherit => factory)
|
120
|
+
instance = inherited.build
|
121
|
+
assert_instance_of User, instance.author
|
122
|
+
end
|
123
|
+
|
106
124
|
end
|
107
125
|
|
108
126
|
context "when defining an inheriting factory" do
|
109
127
|
setup do
|
110
|
-
|
128
|
+
Factory.define(:user) do |u|
|
129
|
+
u.first_name "Joe"
|
130
|
+
u.last_name "Bloggs"
|
131
|
+
u.email "joe@example.com"
|
132
|
+
end
|
111
133
|
end
|
112
134
|
|
113
135
|
teardown do
|
114
|
-
Factory.factories
|
136
|
+
Factory.factories = {}
|
115
137
|
end
|
116
138
|
|
117
139
|
should "pass in an instance of the inherited factory as the inherited_factory" do
|
118
|
-
assert_not_nil Factory.factories[:user]
|
119
140
|
instance = Factory.define(:user_with_stuff, :inherit => :user) {}
|
120
141
|
assert_equal Factory.factories[:user], instance.inherited_factory
|
121
142
|
end
|
@@ -307,6 +328,10 @@ class FactoryTest < Test::Unit::TestCase
|
|
307
328
|
@user = Factory(:user)
|
308
329
|
end
|
309
330
|
|
331
|
+
teardown do
|
332
|
+
Factory.factories = {}
|
333
|
+
end
|
334
|
+
|
310
335
|
should "set up five associated post objects" do
|
311
336
|
assert_equal 5, @user.posts.size
|
312
337
|
end
|