1
0
Fork 0
mirror of https://github.com/thoughtbot/shoulda-matchers.git synced 2022-11-09 12:01:38 -05:00

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

* AllowValueMatcher's `matches?` should check that the attribute was actually set to the value passed to it
This commit is contained in:
Melissa Xie 2013-03-26 18:16:04 -04:00
parent 058f39f187
commit 73723cdbc8
3 changed files with 35 additions and 10 deletions

View file

@ -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

View file

@ -53,10 +53,10 @@ module Shoulda # :nodoc:
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 @@ module Shoulda # :nodoc:
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 has_messages?
message_finder.has_messages?
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_no_messages?
!message_finder.has_messages?
end
def errors_for_attribute_match?
@ -108,7 +119,7 @@ module Shoulda # :nodoc:
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

View file

@ -79,6 +79,13 @@ describe Shoulda::Matchers::ActiveModel::EnsureInclusionOfMatcher do
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 @@ describe Shoulda::Matchers::ActiveModel::EnsureInclusionOfMatcher do
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