Class: RSpec::SleepingKingStudios::Matchers::BuiltIn::RespondToMatcher

Inherits:
Matchers::BuiltIn::RespondTo
  • Object
show all
Includes:
Shared::MatchParameters
Defined in:
lib/rspec/sleeping_king_studios/matchers/built_in/respond_to_matcher.rb

Overview

Extensions to the built-in RSpec #respond_to matcher.

Instance Method Summary collapse

Methods included from Shared::MatchParameters

#argument, #with, #with_a_block, #with_arbitrary_keywords, #with_keywords, #with_unlimited_arguments

Constructor Details

#initialize(*expected) ⇒ RespondToMatcher

Returns a new instance of RespondToMatcher.



12
13
14
15
16
# File 'lib/rspec/sleeping_king_studios/matchers/built_in/respond_to_matcher.rb', line 12

def initialize *expected
  @include_all = [true, false].include?(expected.last) ? expected.pop : false

  super(*expected)
end

Instance Method Details

#descriptionObject



19
20
21
22
23
24
25
26
27
# File 'lib/rspec/sleeping_king_studios/matchers/built_in/respond_to_matcher.rb', line 19

def description
  message = "respond to #{pp_names}"

  if method_signature_expectation?
    message << ' ' << method_signature_expectation.description
  end # if

  message
end

#failure_messageObject

Message for when the object does not match, but was expected to. Make sure to always call #matches? first to set up the matcher state.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rspec/sleeping_king_studios/matchers/built_in/respond_to_matcher.rb', line 30

def failure_message
  method_names = @failing_method_names || []
  messages     = []

  method_names.map do |method_name|
    message = "expected #{@actual.inspect} to respond to #{method_name.inspect}"
    reasons = @failing_method_reasons[method_name] || {}

    if reasons.key?(:does_not_respond_to_method)
      message << ", but #{@actual.inspect} does not respond to #{method_name.inspect}"
    elsif reasons.key?(:is_not_a_method)
      message << ", but #{@actual.inspect} does not define a method at #{method_name.inspect}"
    else
      errors = @failing_method_reasons[method_name]

      # TODO: Replace this with ", but received arguments did not match "\
      # " method signature:"
      message << " with arguments:\n" << format_errors(errors)
    end # if-elsif-else

    messages << message
  end # method

  messages.join "\n"
end

#failure_message_when_negatedObject

Message for when the object matches, but was expected not to. Make sure to always call #matches? first to set up the matcher state.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rspec/sleeping_king_studios/matchers/built_in/respond_to_matcher.rb', line 57

def failure_message_when_negated
  @failing_method_names ||= []
  methods, messages = @failing_method_names, []

  methods.map do |method|
    message = "expected #{@actual.inspect} not to respond to #{method.inspect}"

    if method_signature_expectation?
      message << ' ' << method_signature_expectation.description
    end # if

    messages << message
  end # method

  messages.join "\n"
end