Permalink
Browse files

Fixes #179: Check that attributes are set properly when testing for m…

…atches

* AllowValueMatcher's `matches?` should check that the attribute was actually set to the value passed to it
  • Loading branch information...
mxie committed Mar 26, 2013
1 parent 0209b3e commit 5768a503a6d1f27c0a3ffb2d2ca9af02dc26a6b7
View
@@ -1,4 +1,6 @@
# HEAD
* `AllowValueMatcher` checks that the right value is used for attempts at
setting the attribute with it
* Assert `class_name` value on real class name for `AssociationMatcher`
* Correct the variable used for `validate_confirmation_of` matcher description
@@ -53,11 +53,7 @@ def strict
def matches?(instance)
@instance = instance
@values_to_match.none? do |value|
@value = value
@instance.send("#{@attribute}=", @value)
errors_match?
end
attributes_set_properly? && errors_match?
end
def failure_message_for_should
@@ -74,8 +70,25 @@ def description
private
attr_accessor :value
def errors_match?
has_messages? && errors_for_attribute_match?
@values_to_match.none? do |current_value|
set_attribute_on_instance(current_value)
has_messages? && errors_for_attribute_match?
end
end
def attributes_set_properly?
@values_to_match.all? do |current_value|
set_attribute_on_instance(current_value)
@instance.send(@attribute.to_sym) == current_value
end
end
def set_attribute_on_instance(current_value)
self.value = current_value
@instance.send("#{@attribute}=", current_value)
end
def has_messages?
@@ -108,7 +121,7 @@ def errors_match_string?
def expectation
includes_expected_message = expected_message ? "to include #{expected_message.inspect}" : ''
[error_source, includes_expected_message, "when #{@attribute} is set to #{@value.inspect}"].join(' ')
[error_source, includes_expected_message, "when #{@attribute} is set to #{value.inspect}"].join(' ')
end
def error_source
@@ -79,6 +79,13 @@
end
context 'an attribute which must be included in an array' do
context 'given an attribute that does not accept strings' do
it 'allows an attribute to be set as an integer' do
validating_integer_inclusion(:in => [0,1,2]).
should ensure_inclusion_of(:attr).in_array([0,1,2])
end
end
it 'accepts with correct array' do
validating_inclusion(:in => %w(one two)).
should ensure_inclusion_of(:attr).in_array(%w(one two))
@@ -164,6 +171,12 @@
end
end
def validating_integer_inclusion(options)
define_model(:example, :attr => :integer) do
validates_inclusion_of :attr, options
end.new
end
def validating_inclusion(options)
define_model(:example, :attr => :string) do
validates_inclusion_of :attr, options

0 comments on commit 5768a50

Please sign in to comment.