123456789_123456789_123456789_123456789_123456789_

Class: RSpec::Matchers::BuiltIn::RespondTo Private

Do not use. This class is for internal use only.
Relationships & Source Files
Namespace Children
Classes:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, BaseMatcher
Instance Chain:
Inherits: RSpec::Matchers::BuiltIn::BaseMatcher
Defined in: rspec-expectations/lib/rspec/matchers/built_in/respond_to.rb

Overview

Provides the implementation for RSpec::Matchers#respond_to. Not intended to be instantiated directly.

Constant Summary

BaseMatcher - Inherited

UNDEFINED

Class Method Summary

BaseMatcher - Inherited

.matcher_name, .new,
.underscore

Borrowed from ActiveSupport.

Instance Attribute Summary

BaseMatcher - Inherited

#actual,
#diffable?

::RSpec::Matchers are not diffable by default.

#expected, #expects_call_stack_jump?, #matcher_name, #matcher_name=, #rescued_exception,
#supports_block_expectations?

Most matchers are value matchers (i.e. meant to work with ‘expect(value)`) rather than block matchers (i.e. meant to work with `expect { }`), so this defaults to false.

#supports_value_expectations?

BaseMatcher::StringEncodingFormatting - Included

Instance Method Summary

BaseMatcher - Inherited

#actual_formatted,
#description

Generates a description using ::RSpec::Matchers::EnglishPhrasing.

#expected_formatted,
#match_unless_raises

Used to wrap a block of code that will indicate failure by raising one of the named exceptions.

#matches?

Indicates if the match is successful.

BaseMatcher::DefaultFailureMessages - Included

#failure_message

Provides a good generic failure message.

#failure_message_when_negated

Provides a good generic negative failure message.

BaseMatcher::StringEncodingFormatting - Included

#format_encoding

Formats a String’s encoding as a human readable string :nocov:

BaseMatcher::HashFormatting - Included

#improve_hash_formatting

‘{ :a => 5, :b => 2 }.inspect` produces:

::RSpec::Matchers::Composable - Included

#&

Alias for Composable#and.

#===

Delegates to #matches?.

#and

Creates a compound and expectation.

#description_of

Returns the description of the given object in a way that is aware of composed matchers.

#or

Creates a compound or expectation.

#values_match?

This provides a generic way to fuzzy-match an expected value against an actual value.

#|

Alias for Composable#or.

#should_enumerate?

We should enumerate arrays as long as they are not recursive.

#surface_descriptions_in

Transforms the given data structure (typically a hash or array) into a new data structure that, when #inspect is called on it, will provide descriptions of any contained matchers rather than the normal #inspect output.

#unreadable_io?,
#with_matchers_cloned

Historically, a single matcher instance was only checked against a single value.

Instance Method Details

#and_any_keywords

Alias for #with_any_keywords.

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/respond_to.rb', line 52

alias :and_any_keywords :with_any_keywords

#and_keywords(*keywords)

Alias for #with_keywords.

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/respond_to.rb', line 40

alias :and_keywords :with_keywords

#and_unlimited_arguments

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/respond_to.rb', line 64

alias :and_unlimited_arguments :with_unlimited_arguments

#argument Also known as: #arguments

No-op. Intended to be used as syntactic sugar when using #with.

Examples:

expect(obj).to respond_to(:message).with(3).arguments
[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/respond_to.rb', line 71

def argument
  self
end

#arguments

Alias for #argument.

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/respond_to.rb', line 74

alias :arguments :argument

#descriptionString

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/respond_to.rb', line 100

def description
  "respond to #{pp_names}#{with_arity}"
end

#does_not_match?(actual) ⇒ Boolean

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/respond_to.rb', line 82

def does_not_match?(actual)
  find_failing_method_names(actual, :select).empty?
end

#failure_messageString

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/respond_to.rb', line 88

def failure_message
  "expected #{actual_formatted} to respond to #{@failing_method_names.map { |name| description_of(name) }.join(', ')}#{with_arity}"
end

#failure_message_when_negatedString

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/respond_to.rb', line 94

def failure_message_when_negated
  failure_message.sub(/to respond to/, 'not to respond to')
end

#find_failing_method_names(actual, filter_method) (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/respond_to.rb', line 112

def find_failing_method_names(actual, filter_method)
  @actual = actual
  @failing_method_names = @names.__send__(filter_method) do |name|
    @actual.respond_to?(name) && matches_arity?(actual, name)
  end
end

#ignoring_method_signature_failure!

Used by other matchers to suppress a check

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/respond_to.rb', line 106

def ignoring_method_signature_failure!
  @ignoring_method_signature_failure = true
end

#matches?(actual) ⇒ Boolean

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/respond_to.rb', line 77

def matches?(actual)
  find_failing_method_names(actual, :reject).empty?
end

#matches_arity?(actual, name) ⇒ Boolean (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/respond_to.rb', line 119

def matches_arity?(actual, name)
  ArityCheck.new(@expected_arity, @expected_keywords, @arbitrary_keywords, @unlimited_arguments).matches?(actual, name)
rescue NameError
  return true if @ignoring_method_signature_failure
  raise ArgumentError, "The #{matcher_name} matcher requires that " \
                       "the actual object define the method(s) in " \
                       "order to check arity, but the method " \
                       "`#{name}` is not defined. Remove the arity " \
                       "check or define the method to continue."
end

#pp_names (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/respond_to.rb', line 156

def pp_names
  @names.length == 1 ? "##{@names.first}" : description_of(@names)
end

#with(n)

Specifies the number of expected arguments.

Examples:

expect(obj).to respond_to(:message).with(3).arguments
[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/respond_to.rb', line 24

def with(n)
  @expected_arity = n
  self
end

#with_any_keywords Also known as: #and_any_keywords

Specifies that the method accepts any keyword, i.e. the method has

a splatted keyword parameter of the form **kw_args.

Examples:

expect(obj).to respond_to(:message).with_any_keywords
[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/respond_to.rb', line 48

def with_any_keywords
  @arbitrary_keywords = true
  self
end

#with_arity (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/respond_to.rb', line 130

def with_arity
  str = ''.dup
  str << " with #{with_arity_string}" if @expected_arity
  str << " #{str.length == 0 ? 'with' : 'and'} #{with_keywords_string}" if @expected_keywords && @expected_keywords.count > 0
  str << " #{str.length == 0 ? 'with' : 'and'} unlimited arguments" if @unlimited_arguments
  str << " #{str.length == 0 ? 'with' : 'and'} any keywords" if @arbitrary_keywords
  str
end

#with_arity_string (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/respond_to.rb', line 139

def with_arity_string
  "#{@expected_arity} argument#{@expected_arity == 1 ? '' : 's'}"
end

#with_keywords(*keywords) Also known as: #and_keywords

Specifies keyword arguments, if any.

Examples:

expect(obj).to respond_to(:message).with_keywords(:color, :shape)

with an expected number of arguments

expect(obj).to respond_to(:message).with(3).arguments.and_keywords(:color, :shape)
[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/respond_to.rb', line 36

def with_keywords(*keywords)
  @expected_keywords = keywords
  self
end

#with_keywords_string (private)

[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/respond_to.rb', line 143

def with_keywords_string
  kw_str = case @expected_keywords.count
           when 1
             @expected_keywords.first.inspect
           when 2
             @expected_keywords.map(&:inspect).join(' and ')
           else
             "#{@expected_keywords[0...-1].map(&:inspect).join(', ')}, and #{@expected_keywords.last.inspect}"
           end

  "keyword#{@expected_keywords.count == 1 ? '' : 's'} #{kw_str}"
end

#with_unlimited_arguments Also known as: #and_unlimited_arguments

Specifies that the number of arguments has no upper limit, i.e. the

method has a splatted parameter of the form *args.

Examples:

expect(obj).to respond_to(:message).with_unlimited_arguments
[ GitHub ]

  
# File 'rspec-expectations/lib/rspec/matchers/built_in/respond_to.rb', line 60

def with_unlimited_arguments
  @unlimited_arguments = true
  self
end