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:
parent
74e76ae7e8
commit
5eb9304489
3 changed files with 24 additions and 0 deletions
|
@ -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
|
||||||
#
|
#
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -24,4 +24,5 @@ class UserTest < Test::Unit::TestCase
|
||||||
:null => true, :primary => false, :scale => nil, :sql_type => 'varchar(255)'
|
:null => true, :primary => false, :scale => nil, :sql_type => 'varchar(255)'
|
||||||
should_require_acceptance_of :eula
|
should_require_acceptance_of :eula
|
||||||
should_require_unique_attributes :email, :scoped_to => :name
|
should_require_unique_attributes :email, :scoped_to => :name
|
||||||
|
should_have_readonly_attributes :name
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue