类: RSpec::Matchers::BuiltIn::RespondTo 私有

继承
BaseMatcher
  • Object
显示全部
定义在
lib/rspec/matchers/built_in/respond_to.rb

概述

此类属于私有 API 的一部分。 尽可能避免使用此类,因为它可能会在将来被移除或修改。

提供 respond_to 的实现。 不打算直接实例化。

常量摘要

BaseMatcher 继承的常量

BaseMatcher::UNDEFINED

实例方法摘要 折叠

BaseMatcher 继承的方法

#diffable?, #expects_call_stack_jump?, #match_unless_raises, #supports_block_expectations?

Composable 包含的方法

#===, #and, #description_of, #or, should_enumerate?, surface_descriptions_in, unreadable_io?, #values_match?

构造函数详细信息

#initialize(*names) ⇒RespondTo

此方法属于私有 API 的一部分。 尽可能避免使用此方法,因为它可能会在将来被移除或修改。

返回 RespondTo 的一个新实例。

10
11
12
13
14
15
16
17
# File 'lib/rspec/matchers/built_in/respond_to.rb', line 10
def initialize(*names)
  @names = names
  @expected_arity = nil
  @expected_keywords = []
  @ignoring_method_signature_failure = false
  @unlimited_arguments = nil
  @arbitrary_keywords = nil
end

实例方法详细信息

#argumentObject 另称: arguments

无操作。 旨在用作使用 with 时的语法糖。

示例

expect(obj).to respond_to(:message).with(3).arguments
71
72
73
# File 'lib/rspec/matchers/built_in/respond_to.rb', line 71
def argument
  self
end

#descriptionString

此方法属于私有 API 的一部分。 尽可能避免使用此方法,因为它可能会在将来被移除或修改。

返回

  • (String)
100
101
102
# File 'lib/rspec/matchers/built_in/respond_to.rb', line 100
def description
  "respond to #{pp_names}#{with_arity}"
end

#failure_messageString

此方法属于私有 API 的一部分。 尽可能避免使用此方法,因为它可能会在将来被移除或修改。

返回

  • (String)
88
89
90
# File '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

此方法属于私有 API 的一部分。 尽可能避免使用此方法,因为它可能会在将来被移除或修改。

返回

  • (String)
94
95
96
# File '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

#ignoring_method_signature_failure!Object

此方法属于私有 API 的一部分。 尽可能避免使用此方法,因为它可能会在将来被移除或修改。

由其他匹配器用来抑制检查

106
107
108
# File 'lib/rspec/matchers/built_in/respond_to.rb', line 106
def ignoring_method_signature_failure!
  @ignoring_method_signature_failure = true
end

#with(n) ⇒Object

指定预期参数的数量。

示例

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

#with_any_keywordsObject 另称: and_any_keywords

指定方法接受任何关键字,即方法具有形如 **kw_args 的星号关键字参数。

示例

expect(obj).to respond_to(:message).with_any_keywords
48
49
50
51
# File 'lib/rspec/matchers/built_in/respond_to.rb', line 48
def with_any_keywords
  @arbitrary_keywords = true
  self
end

#with_keywords(*keywords) ⇒Object 另称: and_keywords

指定关键字参数(如果有)。

示例

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

带有预期数量的参数

expect(obj).to respond_to(:message).with(3).arguments.and_keywords(:color, :shape)
36
37
38
39
# File 'lib/rspec/matchers/built_in/respond_to.rb', line 36
def with_keywords(*keywords)
  @expected_keywords = keywords
  self
end

#with_unlimited_argumentsObject 另称: and_unlimited_arguments

指定参数数量没有上限,即方法具有形如 *args 的星号参数。

示例

expect(obj).to respond_to(:message).with_unlimited_arguments
60
61
62
63
# File 'lib/rspec/matchers/built_in/respond_to.rb', line 60
def with_unlimited_arguments
  @unlimited_arguments = true
  self
end