thoughtbot--shoulda-matchers/lib/shoulda/matchers/active_model.rb

96 lines
4.2 KiB
Ruby
Raw Normal View History

require 'shoulda/matchers/active_model/helpers'
require 'shoulda/matchers/active_model/qualifiers'
require 'shoulda/matchers/active_model/validation_matcher'
require 'shoulda/matchers/active_model/validation_matcher/build_description'
require 'shoulda/matchers/active_model/validator'
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'
require 'shoulda/matchers/active_model/disallow_value_matcher'
require 'shoulda/matchers/active_model/validate_length_of_matcher'
require 'shoulda/matchers/active_model/validate_inclusion_of_matcher'
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'
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'
require 'shoulda/matchers/active_model/validate_numericality_of_matcher'
require 'shoulda/matchers/active_model/numericality_matchers/numeric_type_matcher'
require 'shoulda/matchers/active_model/numericality_matchers/comparison_matcher'
require 'shoulda/matchers/active_model/numericality_matchers/odd_number_matcher'
require 'shoulda/matchers/active_model/numericality_matchers/even_number_matcher'
require 'shoulda/matchers/active_model/numericality_matchers/only_integer_matcher'
require 'shoulda/matchers/active_model/allow_mass_assignment_of_matcher'
require 'shoulda/matchers/active_model/errors'
2013-08-18 17:17:19 +00:00
require 'shoulda/matchers/active_model/have_secure_password_matcher'
module Shoulda
module Matchers
# This mixin provides matchers that are used to test behavior, such as
# validations, that you've added to your ActiveModel (or ActiveRecord)
# objects.
#
2015-09-30 19:19:52 +00:00
# ### Testing conditional validations
#
# 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
# RSpec.describe User, type: :model do
# 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
#
# # Minitest (Shoulda)
# 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
#
module ActiveModel
end
end
end