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
refactored should_protect_attributes to just test that the AR model sees 
the attribute as protected.

git-svn-id: https://svn.thoughtbot.com/plugins/shoulda/trunk@371 
7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
tsaleh (author)
Thu Feb 28 14:10:06 -0800 2008
commit  be4c51e7e2b1b65f8cbd47bda0dc00adda331f7b
tree    fc56b5369aa3e18e122f43ba56c0b8b814f30bd3
parent  c397d016f8352e794e725e6cd6005e1e063d1aa8
...
101
102
103
104
105
106
107
108
109
110
111
 
 
 
 
112
113
114
...
285
286
287
288
 
 
 
289
290
291
...
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
 
 
 
346
 
347
348
349
...
373
374
375
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
376
377
378
...
101
102
103
 
 
 
 
 
 
 
 
104
105
106
107
108
109
110
...
281
282
283
 
284
285
286
287
288
289
...
325
326
327
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
328
329
330
331
332
333
334
335
...
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
0
@@ -101,14 +101,10 @@ module ThoughtBot # :nodoc:
0
         klass = model_class
0
         attributes.each do |attribute|
0
           attribute = attribute.to_sym
0
- should "not allow #{attribute} to be changed by update" do
0
- assert object = klass.find(:first), "Can't find first #{klass}"
0
- value = object[attribute]
0
- # TODO: 1 may not be a valid value for the attribute (due to validations)
0
- assert object.update_attributes({ attribute => 1 }),
0
- "Cannot update #{klass} with { :#{attribute} => 1 }, #{object.errors.full_messages.to_sentence}"
0
- assert object.valid?, "#{klass} isn't valid after changing #{attribute}"
0
- assert_equal value, object[attribute], "Was able to change #{klass}##{attribute}"
0
+ should "protect #{attribute} from mass updates" do
0
+ protected = klass.protected_attributes
0
+ assert protected.include?(attribute.to_s),
0
+ "#{klass} is protecting #{protected.to_a.to_sentence}, but not #{attribute}."
0
           end
0
         end
0
       end
0
@@ -285,7 +281,9 @@ module ThoughtBot # :nodoc:
0
         end
0
       end
0
 
0
- # Ensures that the has_many relationship exists.
0
+ # Ensures that the has_many relationship exists. Will also test that the
0
+ # associated table has the required columns. Works with polymorphic
0
+ # associations.
0
       #
0
       # Options:
0
       # * <tt>:through</tt> - association name for <tt>has_many :through</tt>
0
@@ -327,23 +325,11 @@ module ThoughtBot # :nodoc:
0
         end
0
       end
0
 
0
- # Ensures that the has_and_belongs_to_many relationship exists.
0
- #
0
- # should_have_and_belong_to_many :posts, :cars
0
- #
0
- def should_have_and_belong_to_many(*associations)
0
- get_options!(associations)
0
- klass = model_class
0
- associations.each do |association|
0
- should "should have and belong to many #{association}" do
0
- assert klass.reflect_on_association(association), "#{klass.name} does not have any relationship to #{association}"
0
- assert_equal :has_and_belongs_to_many, klass.reflect_on_association(association).macro
0
- end
0
- end
0
- end
0
-
0
- # Ensure that the has_one relationship exists.
0
+ # Ensure that the has_one relationship exists. Will also test that the
0
+ # associated table has the required columns. Works with polymorphic
0
+ # associations.
0
       #
0
+ # Example:
0
       # should_have_one :god # unless hindu
0
       #
0
       def should_have_one(*associations)
0
@@ -373,6 +359,21 @@ module ThoughtBot # :nodoc:
0
         end
0
       end
0
   
0
+ # Ensures that the has_and_belongs_to_many relationship exists.
0
+ #
0
+ # should_have_and_belong_to_many :posts, :cars
0
+ #
0
+ def should_have_and_belong_to_many(*associations)
0
+ get_options!(associations)
0
+ klass = model_class
0
+ associations.each do |association|
0
+ should "should have and belong to many #{association}" do
0
+ assert klass.reflect_on_association(association), "#{klass.name} does not have any relationship to #{association}"
0
+ assert_equal :has_and_belongs_to_many, klass.reflect_on_association(association).macro
0
+ end
0
+ end
0
+ end
0
+
0
       # Ensure that the belongs_to relationship exists.
0
       #
0
       # should_belong_to :parent

Comments

    No one has commented yet.