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
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_formatted} to respond to #{@failing_method_names.map { |name| description_of(name) }.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
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 65 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 72 def matches_arity?(actual, name) return true unless @expected_arity signature = Support::MethodSignature.new(Support.method_handle_for(actual, name)) Support::StrictSignatureVerifier.new(signature, Array.new(@expected_arity)).valid? end
pp_names()
click to toggle source
# File lib/rspec/matchers/built_in/respond_to.rb, line 84 def pp_names @names.length == 1 ? "##{@names.first}" : description_of(@names) end
with_arity()
click to toggle source
# File lib/rspec/matchers/built_in/respond_to.rb, line 79 def with_arity return "" unless @expected_arity " with #{@expected_arity} argument#{@expected_arity == 1 ? '' : 's'}" end