public
Rubygem
Description: Makes tests easy on the fingers and the eyes
Homepage: http://www.thoughtbot.com/projects/shoulda
Clone URL: git://github.com/thoughtbot/shoulda.git
Enhanced assert_good/bad_value assertions in preparation for stateful AR 
validations
rmm5t (author)
Wed Jul 30 21:04:05 -0700 2008
commit  5196f3a98f49519233a7507ef03510761f822b74
tree    38214c3c3ad862d473a6938d9ddb1a6829731826
parent  abc3aa73a49502c874d4b3b184d1e94d075492a0
...
182
183
184
185
186
187
 
 
 
 
 
 
 
 
 
 
 
 
 
 
188
189
190
...
194
195
196
197
198
199
 
 
 
 
 
 
 
 
 
 
 
 
 
 
200
201
202
...
206
207
208
 
 
 
 
 
 
 
 
 
 
 
209
210
211
...
182
183
184
 
 
 
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
...
205
206
207
 
 
 
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
...
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
0
@@ -182,9 +182,20 @@ module ThoughtBot # :nodoc:
0
       # <tt>value</tt> by making sure the <tt>error_message_to_avoid</tt> is not
0
       # contained within the list of errors for that attribute.
0
       #
0
- # assert_good_value(User.new, :email, "user@example.com") #=> passes
0
- # assert_good_value(User.new, :ssn, "123456789", /length/) #=> passes
0
- def assert_good_value(object, attribute, value, error_message_to_avoid = //)
0
+ # assert_good_value(User.new, :email, "user@example.com")
0
+ # assert_good_value(User.new, :ssn, "123456789", /length/)
0
+ #
0
+ # If a class is passed as the first argument, a new object will be
0
+ # instantiated before the assertion. If an instance variable exists with
0
+ # the same name as the class (underscored), that object will be used
0
+ # instead.
0
+ #
0
+ # assert_good_value(User, :email, "user@example.com")
0
+ #
0
+ # @product = Product.new(:tangible => false)
0
+ # assert_good_value(Product, :price, "0")
0
+ def assert_good_value(object_or_klass, attribute, value, error_message_to_avoid = //)
0
+ object = get_instance_of(object_or_klass)
0
         object.send("#{attribute}=", value)
0
         object.valid?
0
         assert_does_not_contain(object.errors.on(attribute), error_message_to_avoid, "when set to #{value.inspect}")
0
@@ -194,9 +205,20 @@ module ThoughtBot # :nodoc:
0
       # <tt>value</tt> by making sure the <tt>error_message_to_expect</tt> is
0
       # contained within the list of errors for that attribute.
0
       #
0
- # assert_bad_value(User.new, :email, "invalid") #=> passes
0
- # assert_bad_value(User.new, :ssn, "123", /length/) #=> passes
0
- def assert_bad_value(object, attribute, value, error_message_to_expect = /invalid/)
0
+ # assert_bad_value(User.new, :email, "invalid")
0
+ # assert_bad_value(User.new, :ssn, "123", /length/)
0
+ #
0
+ # If a class is passed as the first argument, a new object will be
0
+ # instantiated before the assertion. If an instance variable exists with
0
+ # the same name as the class (underscored), that object will be used
0
+ # instead.
0
+ #
0
+ # assert_bad_value(User, :email, "invalid")
0
+ #
0
+ # @product = Product.new(:tangible => true)
0
+ # assert_bad_value(Product, :price, "0")
0
+ def assert_bad_value(object_or_klass, attribute, value, error_message_to_expect = /invalid/)
0
+ object = get_instance_of(object_or_klass)
0
         object.send("#{attribute}=", value)
0
         assert !object.valid?, "#{object.class} allowed #{value.inspect} as a value for #{attribute}"
0
         assert object.errors.on(attribute), "There are no errors on #{attribute} after being set to #{value.inspect}"
0
@@ -206,6 +228,17 @@ module ThoughtBot # :nodoc:
0
       def pretty_error_messages(obj)
0
         obj.errors.map { |a, m| "#{a} #{m} (#{obj.send(a).inspect})" }
0
       end
0
+
0
+ private
0
+
0
+ def get_instance_of(object_or_klass)
0
+ if object_or_klass.is_a?(Class)
0
+ klass = object_or_klass
0
+ instance_variable_get("@#{klass.to_s.underscore}") || klass.new
0
+ else
0
+ object_or_klass
0
+ end
0
+ end
0
     end
0
   end
0
 end
...
129
130
131
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
133
134
...
151
152
153
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154
155
...
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
...
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
0
@@ -129,6 +129,20 @@ class HelpersTest < Test::Unit::TestCase # :nodoc:
0
         assert_good_value User.new, :ssn, "x", /length/
0
       end
0
     end
0
+
0
+ should "accept a class as the first argument" do
0
+ assert_good_value User, :email, "good@example.com"
0
+ end
0
+
0
+ context "with an instance variable" do
0
+ setup do
0
+ @product = Product.new(:tangible => true)
0
+ end
0
+
0
+ should "use that instance variable" do
0
+ assert_good_value Product, :price, "9999", /included/
0
+ end
0
+ end
0
   end
0
 
0
   context "assert_bad_value" do
0
@@ -151,5 +165,19 @@ class HelpersTest < Test::Unit::TestCase # :nodoc:
0
         assert_bad_value User.new, :ssn, "xxxxxxxxx", /length/
0
       end
0
     end
0
+
0
+ should "accept a class as the first argument" do
0
+ assert_bad_value User, :email, "bad"
0
+ end
0
+
0
+ context "with an instance variable" do
0
+ setup do
0
+ @product = Product.new(:tangible => true)
0
+ end
0
+
0
+ should "use that instance variable" do
0
+ assert_bad_value Product, :price, "0", /included/
0
+ end
0
+ end
0
   end
0
 end

Comments

    No one has commented yet.