mirror of
https://github.com/thoughtbot/shoulda-matchers.git
synced 2022-11-09 12:01:38 -05:00
- added should_require_acceptance_of [http://tammer.lighthouseapp.com/projects/5807-shoulda/tickets/6]
git-svn-id: https://svn.thoughtbot.com/plugins/shoulda/trunk@290 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
This commit is contained in:
parent
e1af92ed2f
commit
4f4e52f6d4
4 changed files with 30 additions and 3 deletions
|
@ -455,6 +455,32 @@ module ThoughtBot # :nodoc:
|
|||
end
|
||||
end
|
||||
|
||||
# Ensures that the model cannot be saved if one of the attributes listed is not accepted.
|
||||
#
|
||||
# Options:
|
||||
# * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
|
||||
# Regexp or string. Default = <tt>/must be accepted/</tt>
|
||||
#
|
||||
# Example:
|
||||
# should_require_acceptance_of :eula
|
||||
#
|
||||
def should_require_acceptance_of(*attributes)
|
||||
message = get_options!(attributes, :message)
|
||||
message ||= /must be accepted/
|
||||
klass = model_class
|
||||
|
||||
attributes.each do |attribute|
|
||||
should "require #{attribute} to be accepted" do
|
||||
object = klass.new
|
||||
object.send("#{attribute}=", false)
|
||||
|
||||
assert !object.valid?, "#{klass.name} does not require acceptance of #{attribute}."
|
||||
assert object.errors.on(attribute), "#{klass.name} does not require acceptance of #{attribute}."
|
||||
assert_contains(object.errors.on(attribute), message)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
include ThoughtBot::Shoulda::Private
|
||||
|
|
|
@ -162,8 +162,6 @@ module ThoughtBot # :nodoc:
|
|||
msg += "Body: #{@response.body.first(100).chomp} ..."
|
||||
|
||||
assert_match regex, content_type, msg
|
||||
|
||||
# assert_equal "application/xml", @response.content_type, "Body: #{@response.body.first(100).chomp}..."
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -3,7 +3,9 @@ class User < ActiveRecord::Base
|
|||
has_many :dogs, :foreign_key => :owner_id
|
||||
|
||||
attr_protected :password
|
||||
|
||||
validates_format_of :email, :with => /\w*@\w*.com/
|
||||
validates_length_of :email, :in => 1..100
|
||||
validates_inclusion_of :age, :in => 1..100
|
||||
validates_acceptance_of :eula
|
||||
end
|
||||
|
|
|
@ -5,7 +5,7 @@ class UserTest < Test::Unit::TestCase
|
|||
|
||||
should_have_many :posts
|
||||
should_have_many :dogs
|
||||
|
||||
|
||||
should_not_allow_values_for :email, "blah", "b lah"
|
||||
should_allow_values_for :email, "a@b.com", "asdf@asdf.com"
|
||||
should_ensure_length_in_range :email, 1..100
|
||||
|
@ -17,4 +17,5 @@ class UserTest < Test::Unit::TestCase
|
|||
should_have_db_column :id, :type => "integer", :primary => true
|
||||
should_have_db_column :email, :type => "string", :default => nil, :precision => nil, :limit => 255,
|
||||
:null => true, :primary => false, :scale => nil, :sql_type => 'varchar(255)'
|
||||
should_require_acceptance_of :eula
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue