Commit Graph

15 Commits

Author SHA1 Message Date
Elliot Winkler 583be384c3 Refactor allow_value & disallow_value
This is part of a collection of commits that aim to improve failure
messages across the board, in order to make matchers easier to debug
when something goes wrong.

* Have the failure message describe more clearly what the `allow_value`
  matcher was trying to do when it failed.
* Make the description of the matcher more readable.
* For each value that `allow_value` sets, use a different Validator
  instance. The matcher still changes state as it runs, but a future
  commit will refactor this further.
* Merge StrictValidator back into Validator, and remove it. The way that
  StrictValidator worked (as a module that was mixed into an instance of
  Validator at runtime) was confusing, and there's really no need to
  split out the logic anymore.
* Fix or fill in tests involving failure messages and descriptions.
2015-12-13 20:22:22 -07:00
Elliot Winkler 9ba21381d7 Handle RangeErrors emitted now in ActiveRecord 4.2
In Rails 4.2, ActiveRecord was changed such that if you attempt to set
an attribute to a value and that value is outside the range of the
column, then it will raise a RangeError. For instance, an integer column
with a limit of 2 (i.e. a smallint) only accepts values between -32768
and +32767.

This means that if you try to do any of these three things, a RangeError
could be raised:

* Use validate_numericality_of along with any of the comparison
  submatchers and a value that sits on either side of the boundary.
* Use allow_value with a value that sits outside the range.
* Use validates_inclusion_of against an integer column. (Here we attempt
  to set that column to a non-integer value to verify that the attribute
  does not allow said value. That value is really a string version of a
  large number, so if the column does not take large numbers then the
  matcher could blow up.)

Ancillary changes in this commit:

* Remove ValidationMessageFinder and ExceptionMessageFinder in favor of
  Validator, StrictValidator, and ValidatorWithCapturedRangeError.
* The allow_value matcher now uses an instance of Validator under the
  hood. StrictValidator and/or ValidatorWithCapturedRangeError may be
  mixed into the Validator object as needed.
2015-01-22 21:05:09 -07:00
Elliot Winkler a23081f759 Tweak allow_value failure_message
This commit changes the failure message that `allow_value` generates so
that it reads a bit nicer.

When a call to #valid? resulted in validation messages, `allow_value`
formerly failed with this message:

    Expected errors to include "the message" when attr is set to "some value", got errors: ["another message (attribute: \"attr\", value: \"some value\")", "some other message (attribute: \"attr2\", value: \"some other value\")"]

Now it fails with this message:

    Expected errors to include "the message" when attr is set to "some value",
    got errors:
    * "another message" (attribute: attr, value: "some value")
    * "some other message" (attribute: attr2, value: "some other value")

Similarly, when a call to #valid resulted in an exception, `allow_value`
formerly failed with this message:

    Expected errors to include "the message" when attr is set to "some value", got: some message

Now it fails with this message:

    Expected errors to include "the message" when attr is set to "some value",
    got: "some message"
2014-10-17 11:28:47 -06:00
Elliot Winkler 2a7389ef69 Tweak failure message for allow_value
allow_value currently gives an error message that looks like this:

    Expected errors to include "must be greater than 0" when amount is set to 0, got errors: ["amount must be greater than 0.0 (0)"]

This is confusing because the error message here is "must be greater than 0.0",
not "amount must be greater than 0.0".

With this commit the message changes to:

    Expected errors to include "must be greater than 0" when amount is set to 0, got errors: ["must be greater than 0.0 (attribute: "amount", value: 0)"]
2014-06-27 14:39:01 -06:00
Elliot Winkler c22d7c89e0 Extract examples in README to inline documentation 2014-06-20 16:41:27 -06:00
Yukio Mizuta 90c4a7d215 Use __send__ instead of send 2014-02-01 13:09:44 -08:00
Elliot Winkler fcbb5c485e Convert to 1.9 hash syntax 2014-01-17 13:20:44 -07:00
Henning Koch d1dc606cea Fix a bug in the pretty_error_messages helper.
* Fixes an issue where matchers would fail on models
with an autosaved belongs_to association.
2013-04-26 12:54:10 -04:00
Alan Heywood d92d548d42 Allow the use of %{attribute} or %{model} in i18n translations.
Fix a bug where shoulda was raising an exception, when a translation
in en.yml was modified to include %{attribute} or %{model}
2013-04-12 17:10:18 -04:00
Daniel Duvall 3dd52fbd61 Refactored delegation of error message generation
Moved delegation to ActiveModel::Errors#generate_message to
default_error_message and implemented a spec for it.
2013-03-08 17:14:08 -05:00
Gabe Berke-Williams b9db001879 Make the code easier to read. 2012-04-10 22:55:33 -04:00
Gabe Berke-Williams c65e43a999 Depend on activesupport >= 3.0.0.
Remove some Rails 2.1.x code.
2012-04-10 22:53:07 -04:00
Gabe Berke-Williams 7b3d6d0f86 Continue cleaning up matchers. 2012-03-30 11:50:59 -04:00
Clemens Helm 662b674cf4 Implemented all possible i18n error messages for validations. 2011-11-17 11:57:57 +01:00
Markus Schwed 2a54fdb28d Seperate ActiveRecord and ActiveModel related matchers 2011-05-06 15:56:36 +02:00
Renamed from lib/shoulda/matchers/active_record/helpers.rb (Browse further)