1
0
Fork 0
mirror of https://github.com/thoughtbot/shoulda-matchers.git synced 2022-11-09 12:01:38 -05:00

Added should_have_readonly_attributes

Added should_have_readonly_attributes ActiveRecord helper to
test attributes set with attr_readonly.
This commit is contained in:
Tammer Saleh 2008-06-26 17:15:17 -04:00
commit 9f0c2e10ad
3 changed files with 25 additions and 0 deletions

View file

@ -129,6 +129,28 @@ module ThoughtBot # :nodoc:
end end
end end
# Ensures that the attribute cannot be changed once the record has been created.
# Requires an existing record.
#
# should_have_readonly_attributes :password, :admin_flag
#
def should_have_readonly_attributes(*attributes)
get_options!(attributes)
klass = model_class
attributes.each do |attribute|
attribute = attribute.to_sym
should "make #{attribute} read-only" do
readonly = klass.readonly_attributes || []
assert readonly.include?(attribute.to_s),
(readonly.empty? ?
"#{klass} attribute #{attribute} is not read-only" :
"#{klass} is making #{readonly.to_a.to_sentence} read-only, but not #{attribute}.")
end
end
end
# Ensures that the attribute cannot be set to the given values # Ensures that the attribute cannot be set to the given values
# Requires an existing record # Requires an existing record
# #

View file

@ -5,6 +5,7 @@ class User < ActiveRecord::Base
has_one :address, :as => :addressable has_one :address, :as => :addressable
attr_protected :password attr_protected :password
attr_readonly :name
validates_format_of :email, :with => /\w*@\w*.com/ validates_format_of :email, :with => /\w*@\w*.com/
validates_length_of :email, :in => 1..100 validates_length_of :email, :in => 1..100

View file

@ -27,4 +27,6 @@ class UserTest < Test::Unit::TestCase
should_ensure_length_is :ssn, 9, :message => "Social Security Number is not the right length" should_ensure_length_is :ssn, 9, :message => "Social Security Number is not the right length"
should_only_allow_numeric_values_for :ssn should_only_allow_numeric_values_for :ssn
should_have_readonly_attributes :name
end end