Class: Stannum::RSpec::MatchErrorsMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/stannum/rspec/match_errors_matcher.rb

Overview

Asserts that the expected and actual errors are equal.

Instance Method Summary collapse

Constructor Details

#initialize(expected) ⇒ MatchErrorsMatcher

Returns a new instance of MatchErrorsMatcher.

Parameters:



19
20
21
# File 'lib/stannum/rspec/match_errors_matcher.rb', line 19

def initialize(expected)
  @expected = expected.to_a
end

Instance Method Details

#descriptionString

Returns a short description of the matcher and expected properties.

Returns:

  • (String)

    a short description of the matcher and expected properties.



25
26
27
# File 'lib/stannum/rspec/match_errors_matcher.rb', line 25

def description
  'match the expected errors'
end

#does_not_match?(actual) ⇒ Boolean

Checks that the given errors do not match the expected errors.

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
# File 'lib/stannum/rspec/match_errors_matcher.rb', line 30

def does_not_match?(actual)
  @actual = actual.is_a?(Stannum::Errors) ? actual.to_a : actual

  errors? && equality_matcher.does_not_match?(@actual)
rescue NoMethodError
  # :nocov:
  errors? && !equality_matcher.matches?(@actual)
  # :nocov:
end

#failure_messageString

Returns a summary message describing a failed expectation.

Returns:

  • (String)

    a summary message describing a failed expectation.



41
42
43
44
45
46
47
48
# File 'lib/stannum/rspec/match_errors_matcher.rb', line 41

def failure_message
  unless errors?
    return 'expected the errors to match the expected errors, but the ' \
           'object is not an array or Errors object'
  end

  equality_matcher.failure_message
end

#failure_message_when_negatedString

Returns a summary message describing a failed negated expectation.

Returns:

  • (String)

    a summary message describing a failed negated expectation.



52
53
54
55
56
57
58
59
# File 'lib/stannum/rspec/match_errors_matcher.rb', line 52

def failure_message_when_negated
  unless errors?
    return 'expected the errors not to match the expected errors, but ' \
           'the object is not an array or Errors object'
  end

  equality_matcher.failure_message_when_negated
end

#matches?(actual) ⇒ Boolean

Checks that the given errors match the expected errors.

Returns false if the object is not a Stannum::Errors instance or an Array. Otherwise, it converts the expected and actual errors to arrays and performs a deep match.

Parameters:

  • actual (Object)

    The actual object to match.

Returns:

  • (Boolean)

    true if the actual errors match the expected errors.



70
71
72
73
74
# File 'lib/stannum/rspec/match_errors_matcher.rb', line 70

def matches?(actual)
  @actual = actual.is_a?(Stannum::Errors) ? actual.to_a : actual

  errors? && equality_matcher.matches?(@actual)
end