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

106 lines
3.0 KiB
Ruby
Raw Normal View History

module Shoulda
2010-12-15 22:34:19 +00:00
module Matchers
module ActiveModel
# The `validate_acceptance_of` matcher tests usage of the
# `validates_acceptance_of` validation.
#
# class Registration
# include ActiveModel::Model
# attr_accessor :eula
#
# validates_acceptance_of :eula
# end
#
# # RSpec
# RSpec.describe Registration, type: :model do
# it { should validate_acceptance_of(:eula) }
# end
#
# # Minitest (Shoulda)
# class RegistrationTest < ActiveSupport::TestCase
# should validate_acceptance_of(:eula)
# end
#
# #### Qualifiers
2010-12-15 22:34:19 +00:00
#
# ##### on
#
# Use `on` if your validation applies only under a certain context.
#
# class Registration
# include ActiveModel::Model
# attr_accessor :terms_of_service
#
# validates_acceptance_of :terms_of_service, on: :create
# end
#
# # RSpec
# RSpec.describe Registration, type: :model do
# it do
# should validate_acceptance_of(:terms_of_service).
# on(:create)
# end
# end
#
# # Minitest (Shoulda)
# class RegistrationTest < ActiveSupport::TestCase
# should validate_acceptance_of(:terms_of_service).on(:create)
# end
#
# ##### with_message
2010-12-15 22:34:19 +00:00
#
# Use `with_message` if you are using a custom validation message.
#
# class Registration
# include ActiveModel::Model
# attr_accessor :terms_of_service
#
# validates_acceptance_of :terms_of_service,
# message: 'You must accept the terms of service'
# end
#
# # RSpec
# RSpec.describe Registration, type: :model do
# it do
# should validate_acceptance_of(:terms_of_service).
# with_message('You must accept the terms of service')
# end
# end
#
# # Minitest (Shoulda)
# class RegistrationTest < ActiveSupport::TestCase
# should validate_acceptance_of(:terms_of_service).
# with_message('You must accept the terms of service')
# end
#
# @return [ValidateAcceptanceOfMatcher]
2010-12-15 22:34:19 +00:00
#
def validate_acceptance_of(attr)
ValidateAcceptanceOfMatcher.new(attr)
end
# @private
class ValidateAcceptanceOfMatcher < ValidationMatcher
def initialize(attribute)
super
@expected_message = :accepted
2010-12-15 22:34:19 +00:00
end
def matches?(subject)
super(subject)
disallows_value_of(false, @expected_message)
end
Fix negative versions of validation matchers When using a validation matcher in the negative, i.e.: should_not validate_*(...) as opposed to: should validate_*(...) ...it's common to receive the following error: undefined method `attribute_setter' for nil:NilClass This happens particularly when using a matcher that makes use of AllowValueMatcher or DisallowValueMatcher internally (which all of the validation matchers do). Whenever you make an assertion by using a matcher in the negative as opposed to the positive, RSpec still calls the `matches?` method for that matcher; however, the assertion will pass if that returns *false* as opposed to true. In other words, it just inverts the result. However, whenever we are using AllowValueMatcher or DisallowValueMatcher, it doesn't really work to invert the result. like this. This is because AllowValueMatcher and DisallowValueMatcher, despite their name, aren't truly opposites of each other. AllowValueMatcher performs these steps: 1. Set the attribute on the record to some value 2. Run validations on the record 3. Ask whether validations pass or fail 4. If validations fail, store the value that caused the failure along with the validation errors and return false 5. Otherwise, return true However, DisallowValueMatcher performs these steps: 1. Set the attribute on the record to some value 2. Run validations on the record 3. Ask whether validations pass or fail 4. If validations *pass*, store the value that caused the failure along with some metadata and return false 5. Otherwise, return true This difference in logic is achieved by having AllowValueMatcher implement `does_not_match?` and then having DisallowValueMatcher use this for its positive case and use `matches?` for its negative case. It's easy to see because of this that `does_not_match?` is not the same as `!matches?` and vice versa. So a matcher that makes use of these submatchers internally needs to use their opposite versions whenever that matcher is used in the negative case. In other words, all of the matchers need a `does_not_match?` which is like `matches?`, except that all of the logic is inverted, and in all the cases in which AllowValueMatcher is used, DisallowValueMatcher needs to be used. Doing this ensures that when `failure_message` is called on AllowValueMatcher or DisallowValueMatcher, step 4 in the list of steps above stores a proper value that can then be referenced in the failure message for the validation matcher itself.
2018-09-08 20:59:24 +00:00
def does_not_match?(subject)
super(subject)
allows_value_of(false, @expected_message)
end
def simple_description
%(validate that :#{@attribute} has been set to "1")
2010-12-15 22:34:19 +00:00
end
end
end
end
end