From 8fa97b4ff33b57ce16dfb96be1ec892502f2aa9e Mon Sep 17 00:00:00 2001 From: Elliot Winkler Date: Sun, 4 Oct 2015 16:25:32 -0600 Subject: [PATCH] Fix inclusion matcher w/ date & datetime attrs Why: * The inclusion matcher (when used with the `in_array` qualifier) makes the assertion that when the attribute is set to a value that is outside the given array, the record in question is invalid. The issue is that when used with a date or datetime attribute, the arbitrary value the matcher chose was a string. This was getting typecast and so the matcher was throwing a CouldNotSetAttributeError. To satisfy the above: * If the column is a date, use a Date for the arbitrary value * If the column is a datetime, use a DateTime for the arbitrary value * If the column is a time, use a Time for the arbitrary value --- NEWS.md | 7 ++ .../validate_inclusion_of_matcher.rb | 20 ++++- .../validate_inclusion_of_matcher_spec.rb | 73 ++++++++++++++++++- 3 files changed, 92 insertions(+), 8 deletions(-) diff --git a/NEWS.md b/NEWS.md index a1eec42fe..57acff639 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,10 @@ +# HEAD + +### Bug fixes + +* Fix `validate_inclusion_of` + `in_array` when used against a date or datetime + attribute so that it does not raise a CouldNotSetAttributeError. + # 3.0.0 ### Backward-incompatible changes diff --git a/lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb b/lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb index 9163b7d66..84ae200d9 100644 --- a/lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb +++ b/lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb @@ -263,9 +263,12 @@ def validate_inclusion_of(attr) # @private class ValidateInclusionOfMatcher < ValidationMatcher - ARBITRARY_OUTSIDE_STRING = 'shouldamatchersteststring' + ARBITRARY_OUTSIDE_STRING = 'shoulda-matchers test string' ARBITRARY_OUTSIDE_FIXNUM = 123456789 ARBITRARY_OUTSIDE_DECIMAL = BigDecimal.new('0.123456789') + ARBITRARY_OUTSIDE_DATE = Date.jd(9999999) + ARBITRARY_OUTSIDE_DATETIME = DateTime.jd(9999999) + ARBITRARY_OUTSIDE_TIME = Time.at(9999999999) BOOLEAN_ALLOWS_BOOLEAN_MESSAGE = <