Permalink
Browse files

Changed the behavior of AllowValue matcher so that should_allow_value…

…s_for fails if there is ANY error on the specified attribute
  • Loading branch information...
1 parent af142c6 commit 0a0bbcdeb3209af854372539b988b23da696aaff @matflores matflores committed with jferris May 15, 2009
@@ -125,7 +125,8 @@ def should_have_readonly_attributes(*attributes)
#
# Options:
# * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
- # Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.invalid')</tt>
+ # Regexp or string. If omitted, the test will pass if there is ANY error in
+ # <tt>errors.on(:attribute)</tt>.
#
# Example:
# should_not_allow_values_for :isbn, "bad 1", "bad 2"
@@ -6,8 +6,8 @@ module Matchers
#
# Options:
# * <tt>with_message</tt> - value the test expects to find in
- # <tt>errors.on(:attribute)</tt>. Regexp or string. Defaults to the
- # translation for :invalid.
+ # <tt>errors.on(:attribute)</tt>. Regexp or string. If omitted,
+ # the test looks for any errors in <tt>errors.on(:attribute)</tt>.
#
# Example:
# it { should_not allow_value('bad').for(:isbn) }
@@ -36,7 +36,6 @@ def with_message(message)
def matches?(instance)
@instance = instance
- @expected_message ||= :invalid
if Symbol === @expected_message
@expected_message = default_error_message(@expected_message)
end
@@ -62,7 +61,7 @@ def errors_match?
@instance.valid?
@errors = @instance.errors.on(@attribute)
@errors = [@errors] unless @errors.is_a?(Array)
- errors_match_regexp? || errors_match_string?
+ @expected_message ? (errors_match_regexp? || errors_match_string?) : (@errors != [nil])
end
def errors_match_regexp?
@@ -84,7 +83,8 @@ def errors_match_string?
end
def expectation
- "errors to include #{@expected_message.inspect} " <<
+ "errors " <<
+ (@expected_message ? "to include #{@expected_message.inspect} " : "") <<
"when #{@attribute} is set to #{@value.inspect}"
end
@@ -38,4 +38,27 @@ class AllowValueMatcherTest < ActiveSupport::TestCase # :nodoc:
end
end
+ context "an attribute with several validations" do
+ setup do
+ define_model :example, :attr => :string do
+ validates_presence_of :attr
+ validates_length_of :attr, :within => 1..5
+ validates_numericality_of :attr, :greater_than_or_equal_to => 1,
+ :less_than_or_equal_to => 50000
+ end
+ @model = Example.new
+ end
+
+ should "allow a good value" do
+ assert_accepts allow_value("12345").for(:attr), @model
+ end
+
+ bad_values = [nil, "", "abc", "0", "50001", "123456"]
+ bad_values.each do |value|
+ should "not allow a bad value (#{value.inspect})" do
+ assert_rejects allow_value(value).for(:attr), @model
+ end
+ end
+ end
+
end
View
@@ -38,6 +38,9 @@ class UserTest < ActiveSupport::TestCase
should_not_allow_values_for :email, "blah", "b lah"
should_allow_values_for :email, "a@b.com", "asdf@asdf.com"
+ should_allow_values_for :age, 1, 10, 99
+ should_not_allow_values_for :age, "a", "-"
+ should_not_allow_values_for :ssn, "a", 1234567890
should_ensure_length_in_range :email, 1..100
should_ensure_value_in_range :age, 1..100, :low_message => /greater/,
:high_message => /less/

0 comments on commit 0a0bbcd

Please sign in to comment.