class RSpec::Matchers::BuiltIn::RespondTo

@api private Provides the implementation for `respond_to`. Not intended to be instantiated directly.

Public Class Methods

new(*names) click to toggle source
# File lib/rspec/matchers/built_in/respond_to.rb, line 10
def initialize(*names)
  @names = names
  @expected_arity = nil
end

Public Instance Methods

argument() click to toggle source

@api public No-op. Intended to be used as syntactic sugar when using `with`.

@example

expect(obj).to respond_to(:message).with(3).arguments
# File lib/rspec/matchers/built_in/respond_to.rb, line 30
def argument
  self
end
Also aliased as: arguments
arguments()
Alias for: argument
description() click to toggle source

@api private @return [String]

# File lib/rspec/matchers/built_in/respond_to.rb, line 59
def description
  "respond to #{pp_names}#{with_arity}"
end
does_not_match?(actual) click to toggle source

@private

# File lib/rspec/matchers/built_in/respond_to.rb, line 41
def does_not_match?(actual)
  find_failing_method_names(actual, :select).empty?
end
failure_message() click to toggle source

@api private @return [String]

# File lib/rspec/matchers/built_in/respond_to.rb, line 47
def failure_message
  "expected #{@actual.inspect} to respond to #{@failing_method_names.collect {|name| name.inspect }.join(', ')}#{with_arity}"
end
failure_message_when_negated() click to toggle source

@api private @return [String]

# File lib/rspec/matchers/built_in/respond_to.rb, line 53
def failure_message_when_negated
  failure_message.sub(/to respond to/, 'not to respond to')
end
matches?(actual) click to toggle source

@private

# File lib/rspec/matchers/built_in/respond_to.rb, line 36
def matches?(actual)
  find_failing_method_names(actual, :reject).empty?
end
supports_block_expectations?() click to toggle source

@private

# File lib/rspec/matchers/built_in/respond_to.rb, line 64
def supports_block_expectations?
  false
end
with(n) click to toggle source

@api public Specifies the number of expected arguments.

@example

expect(obj).to respond_to(:message).with(3).arguments
# File lib/rspec/matchers/built_in/respond_to.rb, line 20
def with(n)
  @expected_arity = n
  self
end

Private Instance Methods

find_failing_method_names(actual, filter_method) click to toggle source
# File lib/rspec/matchers/built_in/respond_to.rb, line 70
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
matches_arity?(actual, name) click to toggle source
# File lib/rspec/matchers/built_in/respond_to.rb, line 77
def matches_arity?(actual, name)
  return true unless @expected_arity

  signature = Support::MethodSignature.new(actual.method(name))
  Support::MethodSignatureVerifier.new(signature, Array.new(@expected_arity)).valid?
end
pp_names() click to toggle source
# File lib/rspec/matchers/built_in/respond_to.rb, line 89
def pp_names
  # Ruby 1.9 returns the same thing for array.to_s as array.inspect, so just use array.inspect here
  @names.length == 1 ? "##{@names.first}" : @names.inspect
end
with_arity() click to toggle source
# File lib/rspec/matchers/built_in/respond_to.rb, line 84
def with_arity
  @expected_arity.nil?? "" :
    " with #{@expected_arity} argument#{@expected_arity == 1 ? '' : 's'}"
end