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...
1 parent 058f39f commit 73723cdbc8a7889987b2bc5209ea31f033d64972 @mxie mxie committed Mar 26, 2013
View
@@ -1,4 +1,9 @@
# HEAD
+* `AllowValueMatcher` checks that the right value is used for attempts at
+setting the attribute with it
+ * Please note that previously-passing tests might now fail. It is likely that
+ it's not a bug, but please make sure that the code you're testing is written
+ properly before submitting an issue.
* Use DisallowValueMatcher for `disallows_value_of` method
* Assert `class_name` value on real class name for `AssociationMatcher`
* Correct the variable used for `validate_confirmation_of` matcher description
@@ -53,10 +53,10 @@ def strict
def matches?(instance)
@instance = instance
- @values_to_match.none? do |value|
- @value = value
- @instance.send("#{@attribute}=", @value)
- errors_match?
+
+ @values_to_match.all? do |current_value|
+ set_attribute_on_instance(current_value)
+ matches_attribute_value?(current_value) && errors_do_not_match?
end
end
@@ -74,12 +74,23 @@ def description
private
- def errors_match?
- has_messages? && errors_for_attribute_match?
+ attr_accessor :value
+
+ def set_attribute_on_instance(current_value)
+ self.value = current_value
+ @instance.send("#{@attribute}=", current_value)
+ end
+
+ def matches_attribute_value?(current_value)
+ @instance.send(@attribute.to_sym) == current_value
+ end
+
+ def errors_do_not_match?
+ has_no_messages? || !errors_for_attribute_match?
end
- def has_messages?
- message_finder.has_messages?
+ def has_no_messages?
+ !message_finder.has_messages?
end
def errors_for_attribute_match?
@@ -108,7 +119,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_inclusion(:in => [0,1,2], :column_type => :integer).
+ 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))
@@ -165,7 +172,9 @@
end
def validating_inclusion(options)
- define_model(:example, :attr => :string) do
+ options[:column_type] ||= :string
+
+ define_model(:example, :attr => options[:column_type]) do
validates_inclusion_of :attr, options
end.new
end

0 comments on commit 73723cd

Please sign in to comment.