Sha256: 1530cae90228eb22aeaed7f643b9e9d5aa5d7dff3bbeb97f096d9c60b1bf2614

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

module MiniTest
  module Matchers
    module ActiveModel
      # Ensures that the model is invalid if the given attribute is not accepted.
      #
      # Options:
      # * <tt>accept_with</tt> - value that is considered accepted.
      #
      #   it { must validate_acceptance_of(:eula) }
      #   it { must validate_acceptance_of(:terms_of_service).accept_with(true) }
      def validate_acceptance_of attr
        ValidateAcceptanceMatcher.new attr
      end

      private

      class ValidateAcceptanceMatcher < ValidationMatcher
        def initialize attr
          @accepted = nil

          super attr, :acceptance
        end

        def accept_with value
          @accepted = value
          self
        end

        def matches? subject
          return false unless @result = super(subject)

          check_accepted_value if @accepted

          @result
        end

        def description
          desc = ''
          desc = " accept with #{@accepted.inspect}" if @accepted
          super << desc
        end

        private

        def check_accepted_value
          actual = @validator.options[:accept]

          if actual == @accepted
            @positive_message << " accept with #{actual.inspect}."
          else
            @negative_message << " accept with #{actual.inspect}."
            @result = false
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
minitest-activemodel-1.1.0 lib/matchers/validate_acceptance_matcher.rb
minitest-activemodel-1.0.0 lib/matchers/validate_acceptance_matcher.rb