shoulda-matchers icon indicating copy to clipboard operation
shoulda-matchers copied to clipboard

validates_inclusion_of with_message does not respect interpolated value in the message

Open dlinch opened this issue 6 years ago • 1 comments

validate_inclusion_of accepts a message option, inside which you are able to specify the value that was passed to the field for validation. It looks like:

with_message('the value: %{value} is not inside the acceptable list')

Shoulda-matchers does not seem to respect the interpolation of this custom value. It is shown in the method documentation here:

When I try this locally, my code looks like this:

  validates_inclusion_of :rating, in: 1..5, allow_nil: true, message: 'the value: %{value} is not between 1 and 5'

And the error message I receive is:

Expected Survey to validate that :rating lies inside the
       range ‹1› to ‹5› as long as it is not nil, producing a custom validation
       error on failure, but this could not be proved.
         After setting :rating to ‹0›, the matcher expected the
         Survey to be invalid and to produce the validation error "the value:
         %{value} is not between 1 and 5" on :rating. The record was
         indeed invalid, but it produced these validation errors instead:
     
         * rating: ["the value: 0 is not between 1 and 5"]

I believe this is an error in the shoulda-matchers code, as it is not interpolating the value correctly with the error message.

dlinch avatar Jul 16 '19 21:07 dlinch

I am encounter the same issue, currently just use regex to test the message. Hope there will be a fix coming out soon

GeneDer avatar Jul 26 '19 03:07 GeneDer