类: RSpec::Matchers::AliasedMatcher 私有

继承
MatcherDelegator
  • Object
显示全部
定义于
lib/rspec/matchers/aliased_matcher.rb

概述

此类是私有 API 的一部分。 应尽可能避免使用此类,因为它可能在将来被删除或更改。

装饰器,它包装一个匹配器并使用提供的代码块覆盖description,以支持匹配器的别名。 这是为了在组合匹配器时使用,这样你就可以使用像include( a_value_within(0.1).of(3) )这样的表达式,而不是include( be_within(0.1).of(3) ),并具有相应的自然描述。

实例方法摘要 收起

Composable 中包含的方法

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

构造函数详情

#initialize(base_matcher, description_block) ⇒AliasedMatcher

此方法是私有 API 的一部分。 应尽可能避免使用此方法,因为它可能在将来被删除或更改。

返回一个新的 AliasedMatcher 实例。

13
14
15
16
# File 'lib/rspec/matchers/aliased_matcher.rb', line 13
def initialize(base_matcher, description_block)
  @description_block = description_block
  super(base_matcher)
end

动态方法处理

此类通过以下方法处理动态方法:method_missing方法

#method_missingObject

此方法是私有 API 的一部分。 应尽可能避免使用此方法,因为它可能在将来被删除或更改。

将消息转发到包装的匹配器。 由于许多匹配器提供了流畅的界面(例如a_value_within(0.1).of(3)),因此我们需要包装返回的值(如果它响应description),以便在最终使用时可以应用我们的覆盖。

24
25
26
27
28
# File 'lib/rspec/matchers/aliased_matcher.rb', line 24
def method_missing(*)
  return_val = super
  return return_val unless RSpec::Matchers.is_a_matcher?(return_val)
  self.class.new(return_val, @description_block)
end

实例方法详情

#descriptionObject

此方法是私有 API 的一部分。 应尽可能避免使用此方法,因为它可能在将来被删除或更改。

提供别名匹配器的描述。 别名匹配器旨在与原始匹配器行为相同,除了描述和错误消息。 描述不同以反映别名。

36
37
38
# File 'lib/rspec/matchers/aliased_matcher.rb', line 36
def description
  @description_block.call(super)
end

#failure_messageObject

此方法是私有 API 的一部分。 应尽可能避免使用此方法,因为它可能在将来被删除或更改。

提供别名匹配器的 failure_message。 别名匹配器旨在与原始匹配器行为相同,除了描述和错误消息。 failure_message 不同以反映别名。

46
47
48
# File 'lib/rspec/matchers/aliased_matcher.rb', line 46
def failure_message
  @description_block.call(super)
end

#failure_message_when_negatedObject

此方法是私有 API 的一部分。 应尽可能避免使用此方法,因为它可能在将来被删除或更改。

提供别名匹配器的 failure_message_when_negated。 别名匹配器旨在与原始匹配器行为相同,除了描述和错误消息。 failure_message_when_negated 不同以反映别名。

56
57
58
# File 'lib/rspec/matchers/aliased_matcher.rb', line 56
def failure_message_when_negated
  @description_block.call(super)
end