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
Added should_have_readonly_attributes

Added should_have_readonly_attributes ActiveRecord helper to
test attributes set with attr_readonly.
boone (author)
Tue Jun 24 12:50:38 -0700 2008
commit  5eb9304489af5cb0314c971b55fe6fb75a0741b4
tree    da6b4a6eb89d1598b1ce8ce9b6c42b480175f8f2
parent  74e76ae7e846a15ea5809293eccda551e9cdedc4
...
129
130
131
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
133
134
...
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
0
@@ -129,6 +129,28 @@ module ThoughtBot # :nodoc:
0
         end
0
       end
0
   
0
+ # Ensures that the attribute cannot be changed once the record has been created.
0
+ # Requires an existing record.
0
+ #
0
+ # should_have_readonly_attributes :password, :admin_flag
0
+ #
0
+ def should_have_readonly_attributes(*attributes)
0
+ get_options!(attributes)
0
+ klass = model_class
0
+
0
+ attributes.each do |attribute|
0
+ attribute = attribute.to_sym
0
+ should "make #{attribute} read-only" do
0
+ readonly = klass.readonly_attributes || []
0
+
0
+ assert readonly.include?(attribute.to_s),
0
+ (readonly.empty? ?
0
+ "#{klass} attribute #{attribute} is not read-only" :
0
+ "#{klass} is making #{readonly.to_a.to_sentence} read-only, but not #{attribute}.")
0
+ end
0
+ end
0
+ end
0
+
0
       # Ensures that the attribute cannot be set to the given values
0
       # Requires an existing record
0
       #
...
5
6
7
 
8
9
10
...
5
6
7
8
9
10
11
0
@@ -5,6 +5,7 @@ class User < ActiveRecord::Base
0
   has_one :address, :as => :addressable
0
   
0
   attr_protected :password
0
+ attr_readonly :name
0
 
0
   validates_format_of :email, :with => /\w*@\w*.com/
0
   validates_length_of :email, :in => 1..100
...
24
25
26
 
27
...
24
25
26
27
28
0
@@ -24,4 +24,5 @@ class UserTest < Test::Unit::TestCase
0
                                 :null => true, :primary => false, :scale => nil, :sql_type => 'varchar(255)'
0
   should_require_acceptance_of :eula
0
   should_require_unique_attributes :email, :scoped_to => :name
0
+ should_have_readonly_attributes :name
0
 end

Comments

    No one has commented yet.