Class: MiniTest::Matchers::ActiveModel::ValidationMatcher

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/matchers/validation_matcher.rb

Overview

:nodoc:

Instance Method Summary collapse

Methods included from Helpers

#class_of, #to_sentence

Constructor Details

#initialize(attr, type) ⇒ ValidationMatcher

Returns a new instance of ValidationMatcher


7
8
9
10
11
# File 'lib/matchers/validation_matcher.rb', line 7

def initialize attr, type
  @attr = attr
  @type = type
  @expected_on = @expected_message = nil
end

Instance Method Details

#descriptionObject


45
46
47
# File 'lib/matchers/validation_matcher.rb', line 45

def description
  "validate #{@type} of #{@attr}"
end

#failure_messageObject


37
38
39
# File 'lib/matchers/validation_matcher.rb', line 37

def failure_message
  "Expected #{@klass} to #{description}; instead got #{@negative_message}"
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)

25
26
27
28
29
30
31
32
33
34
35
# File 'lib/matchers/validation_matcher.rb', line 25

def matches? subject
  @klass     = subject.is_a?(Class) ? subject : subject.class
  @validator = @klass.validators_on(@attr).find { |v| v.kind == @type }
  @result    = true

  check_validator
  check_on        if @expected_on
  check_message   if @expected_message

  @result
end

#negative_failure_messageObject


41
42
43
# File 'lib/matchers/validation_matcher.rb', line 41

def negative_failure_message
  "Expected #{@klass} to not #{description}; instead got #{@positive_message}"
end

#on(*contexts) ⇒ Object

TODO: Add documentation.


14
15
16
17
# File 'lib/matchers/validation_matcher.rb', line 14

def on *contexts
  @expected_on = clean_contexts contexts
  self
end

#with_message(expected_message) ⇒ Object

TODO: Add documentation.


20
21
22
23
# File 'lib/matchers/validation_matcher.rb', line 20

def with_message expected_message
  @expected_message = expected_message
  self
end