2011-05-06 11:51:12 +00:00
|
|
|
require 'shoulda/matchers/active_model/helpers'
|
Add ignoring_interference_by_writer to all matchers
`allow_value` matcher is, of course, concerned with setting values on a
particular attribute on a particular record, and then checking that the
record is valid after doing so. That comes with a caveat: if the
attribute is overridden in such a way so that the same value going into
the attribute isn't the same value coming out of it, then `allow_value`
will balk -- it'll say, "I can't do that because that changes how I
work."
That's all well and good, but what the attribute intentionally changes
incoming values? ActiveRecord's typecasting behavior, for instance,
would trigger such an exception. What if the developer needs a way to
get around this? This is where `ignoring_interference_by_writer` comes
into play. You can tack it on to the end of the matcher, and you're free
to go on your way.
So, prior to this commit you could already apply it to `allow_value`,
but now in this commit it also works on any other matcher.
But, one little thing: sometimes using this qualifier isn't going to
work. Perhaps you or something else actually *is* overriding the
attribute to change incoming values in a specific way, and perhaps the
value that comes out makes the record fail validation, and there's
nothing you can do about it. So in this case, even if you're using
`ignoring_interference_by_writer`, we want to inform you about what the
attribute is doing -- what the input and output was. And so we do.
2015-12-31 01:47:46 +00:00
|
|
|
require 'shoulda/matchers/active_model/qualifiers'
|
2011-05-06 11:51:12 +00:00
|
|
|
require 'shoulda/matchers/active_model/validation_matcher'
|
2015-12-14 00:01:14 +00:00
|
|
|
require 'shoulda/matchers/active_model/validation_matcher/build_description'
|
2015-01-21 22:46:26 +00:00
|
|
|
require 'shoulda/matchers/active_model/validator'
|
2011-05-06 11:51:12 +00:00
|
|
|
require 'shoulda/matchers/active_model/allow_value_matcher'
|
allow_value: pre-set attributes before validation
While attempting to add support for `ignoring_interference_by_writer` to
the confirmation matcher, I was noticing that there are two attributes
we are concerned with: the attribute under test, and the confirmation
attribute -- for instance, `password` and `password_confirmation`. The
way that the matcher works, `password_confirmation` is set first on the
record before `password` is set, and then the whole record is validated.
This is fine, but I also noticed that `allow_value` has a specific way
of setting attributes -- not only does it check whether the attribute
being set exists and fail properly if it is does not, but it also
raises a CouldNotSetAttribute error if the attribute changes incoming
values. This logic needs to be performed on both `password_confirmation`
as well as `password`.
With that in mind, `allow_value` now supports a `values_to_preset=`
writer method which allows one to assign additional attributes unrelated
to the one being tested prior to validation. This will be used by the
confirmation matcher in a future commit.
This means that `allow_value` now operates in two steps:
1. Set attributes unrelated to the test, raising an error if any of the
attributes do not exist on the model.
2. Set the attribute under test to one or more values, raising an error
if the attribute does not exist, then running validations on the
record, failing with an appropriate error message if the validations
fail.
Note that the second step is similar to the first, although there are
more things involved. To that end, `allow_value` has been completely
refactored so that the logic for setting and validating attributes
happens in other places. Specifically, the core logic to set an
attribute (and capture the results) is located in a new AttributeSetter
class.
Also, the CouldNotSetAttributeError class has been moved to a namespace
and renamed to AttributeChangedValueError.
Finally, this commit fixes DisallowValueMatcher so that it is the true
opposite of AllowValueMatcher: DVM#matches? calls AVM#does_not_match?
and DVM#does_not_match? calls AVM#matches?.
2015-12-19 17:11:01 +00:00
|
|
|
require 'shoulda/matchers/active_model/allow_value_matcher/attribute_changed_value_error'
|
|
|
|
require 'shoulda/matchers/active_model/allow_value_matcher/attribute_does_not_exist_error'
|
|
|
|
require 'shoulda/matchers/active_model/allow_value_matcher/attribute_setter'
|
|
|
|
require 'shoulda/matchers/active_model/allow_value_matcher/attribute_setter_and_validator'
|
|
|
|
require 'shoulda/matchers/active_model/allow_value_matcher/attribute_setters'
|
|
|
|
require 'shoulda/matchers/active_model/allow_value_matcher/attribute_setters_and_validators'
|
|
|
|
require 'shoulda/matchers/active_model/allow_value_matcher/successful_check'
|
|
|
|
require 'shoulda/matchers/active_model/allow_value_matcher/successful_setting'
|
2012-10-16 17:45:06 +00:00
|
|
|
require 'shoulda/matchers/active_model/disallow_value_matcher'
|
2014-11-04 21:21:30 +00:00
|
|
|
require 'shoulda/matchers/active_model/validate_length_of_matcher'
|
2014-04-24 04:25:34 +00:00
|
|
|
require 'shoulda/matchers/active_model/validate_inclusion_of_matcher'
|
2014-05-10 08:59:21 +00:00
|
|
|
require 'shoulda/matchers/active_model/validate_exclusion_of_matcher'
|
2013-12-05 16:38:30 +00:00
|
|
|
require 'shoulda/matchers/active_model/validate_absence_of_matcher'
|
2011-05-06 11:51:12 +00:00
|
|
|
require 'shoulda/matchers/active_model/validate_presence_of_matcher'
|
|
|
|
require 'shoulda/matchers/active_model/validate_acceptance_of_matcher'
|
2011-10-23 13:41:37 +00:00
|
|
|
require 'shoulda/matchers/active_model/validate_confirmation_of_matcher'
|
2011-05-06 11:51:12 +00:00
|
|
|
require 'shoulda/matchers/active_model/validate_numericality_of_matcher'
|
2014-01-13 01:43:36 +00:00
|
|
|
require 'shoulda/matchers/active_model/numericality_matchers/numeric_type_matcher'
|
2013-11-01 21:57:42 +00:00
|
|
|
require 'shoulda/matchers/active_model/numericality_matchers/comparison_matcher'
|
2014-01-13 01:43:36 +00:00
|
|
|
require 'shoulda/matchers/active_model/numericality_matchers/odd_number_matcher'
|
|
|
|
require 'shoulda/matchers/active_model/numericality_matchers/even_number_matcher'
|
2013-11-01 21:57:42 +00:00
|
|
|
require 'shoulda/matchers/active_model/numericality_matchers/only_integer_matcher'
|
2011-05-06 11:51:12 +00:00
|
|
|
require 'shoulda/matchers/active_model/allow_mass_assignment_of_matcher'
|
2012-09-28 17:35:15 +00:00
|
|
|
require 'shoulda/matchers/active_model/errors'
|
2013-08-18 17:17:19 +00:00
|
|
|
require 'shoulda/matchers/active_model/have_secure_password_matcher'
|
2011-05-06 11:51:12 +00:00
|
|
|
|
|
|
|
module Shoulda
|
|
|
|
module Matchers
|
2019-02-16 10:01:35 +00:00
|
|
|
# This module provides matchers that are used to test behavior within
|
|
|
|
# ActiveModel or ActiveRecord classes.
|
2015-07-09 22:14:30 +00:00
|
|
|
#
|
2015-09-30 19:19:52 +00:00
|
|
|
# ### Testing conditional validations
|
2015-07-09 22:14:30 +00:00
|
|
|
#
|
|
|
|
# If your model defines a validation conditionally -- meaning that the
|
|
|
|
# validation is declared with an `:if` or `:unless` option -- how do you
|
|
|
|
# test it? You might expect the validation matchers here to have
|
|
|
|
# corresponding `if` or `unless` qualifiers, but this isn't what you use.
|
|
|
|
# Instead, before using the matcher in question, you place the record
|
|
|
|
# you're testing in a state such that the validation you're also testing
|
|
|
|
# will be run. A common way to do this is to make a new `context` and
|
|
|
|
# override the subject to populate the record accordingly. You'll also want
|
|
|
|
# to make sure to test that the validation is *not* run when the
|
|
|
|
# conditional fails.
|
|
|
|
#
|
|
|
|
# Here's an example to illustrate what we mean:
|
|
|
|
#
|
|
|
|
# class User
|
|
|
|
# include ActiveModel::Model
|
|
|
|
#
|
|
|
|
# attr_accessor :role, :admin
|
|
|
|
#
|
|
|
|
# validates_presence_of :role, if: :admin
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# # RSpec
|
2016-06-16 00:00:51 +00:00
|
|
|
# RSpec.describe User, type: :model do
|
2015-07-09 22:14:30 +00:00
|
|
|
# context "when an admin" do
|
|
|
|
# subject { User.new(admin: true) }
|
|
|
|
#
|
|
|
|
# it { should validate_presence_of(:role) }
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# context "when not an admin" do
|
|
|
|
# subject { User.new(admin: false) }
|
|
|
|
#
|
|
|
|
# it { should_not validate_presence_of(:role) }
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
2015-09-30 19:15:23 +00:00
|
|
|
# # Minitest (Shoulda)
|
2015-07-09 22:14:30 +00:00
|
|
|
# class UserTest < ActiveSupport::TestCase
|
|
|
|
# context "when an admin" do
|
|
|
|
# subject { User.new(admin: true) }
|
|
|
|
#
|
|
|
|
# should validate_presence_of(:role)
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# context "when not an admin" do
|
|
|
|
# subject { User.new(admin: false) }
|
|
|
|
#
|
|
|
|
# should_not validate_presence_of(:role)
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
2011-05-06 11:51:12 +00:00
|
|
|
module ActiveModel
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|