模块: RSpec::Matchers::DSL::Macros
- 包含于
- Matcher
- 定义于
- lib/rspec/matchers/dsl.rb
概述
包含在 RSpec::Matchers.define
DSL 中用于创建自定义匹配器的可用方法。
定义于命名空间下
模块: Deprecated
实例方法摘要 收起
-
#chain(method_name, *attr_names, &definition) ⇒ Object
方便在该匹配器上定义方法以创建流畅的接口。
-
#description {|Object| ... } ⇒ Object
自定义用于单行描述的描述。
-
#diffable ⇒ Object
告诉匹配器在失败消息中对实际值和预期值进行差异比较。
-
#failure_message {|Object| ... } ⇒ Object
自定义当要求该匹配器进行正向匹配时使用的失败消息。
-
#failure_message_when_negated {|Object| ... } ⇒ Object
自定义当要求该匹配器进行负向匹配时使用的失败消息。
-
#match(options = {}) {|Object| ... } ⇒ Object
存储用于确定该匹配器是否通过或失败的代码块。
-
#match_unless_raises(expected_exception = Exception) {|Object| ... } ⇒ Object
当代码块引发异常而不是返回 false 以指示失败时,请使用它代替
match
。 -
#match_when_negated(options = {}) {|Object| ... } ⇒ Object
当正向和负向形式需要不同的处理时,使用它来定义负向期望 (
expect(...).not_to
) 的代码块。 -
#supports_block_expectations ⇒ Object
声明该匹配器可以在代码块期望中使用。
实例方法详情
#chain(method_name, *attr_names, &definition) ⇒Object
方便在该匹配器上定义方法以创建流畅的接口。流畅接口的技巧是每个方法必须返回 self 以便将方法链接在一起。chain
会为您处理此操作。如果调用了该方法,并且 include_chain_clauses_in_custom_matcher_descriptions
配置选项哈希已被启用,则链接方法名称和参数将被添加到默认描述和失败消息中。
在您只想让链接方法为以后使用(例如在 match
中)存储一些值(例如)的常见情况下,您可以提供一个或多个属性名称而不是代码块;链接方法将把其参数存储在具有这些名称的实例变量中,并且这些值将通过 getter 暴露。
298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/rspec/matchers/dsl.rb', line 298 def chain(method_name, *attr_names, &definition) unless block_given? ^ attr_names.any? raise ArgumentError, "You must pass either a block or some attribute names (but not both) to `chain`." end definition = assign_attributes(attr_names) if attr_names.any? define_user_override(method_name, definition) do |*args, &block| super(*args, &block) @chained_method_clauses.push([method_name, args]) self end end |
#description {|Object| ... } ⇒Object
自定义用于单行描述的描述。仅当默认生成的描述不适合您的需要时才使用此方法。
253 254 255 |
# File 'lib/rspec/matchers/dsl.rb', line 253 def description(&definition) define_user_override(__method__, definition) end |
#diffable ⇒Object
告诉匹配器在失败消息中对实际值和预期值进行差异比较。
259 260 261 |
# File 'lib/rspec/matchers/dsl.rb', line 259 def diffable define_method(:diffable?) { true } end |
#failure_message {|Object| ... } ⇒Object
自定义当要求该匹配器进行正向匹配时使用的失败消息。仅当默认生成的消息不适合您的需要时才使用此方法。
216 217 218 |
# File 'lib/rspec/matchers/dsl.rb', line 216 def (&definition) define_user_override(__method__, definition) end |
#failure_message_when_negated {|Object| ... } ⇒Object
自定义当要求该匹配器进行负向匹配时使用的失败消息。仅当默认生成的消息不适合您的需要时才使用此方法。
235 236 237 |
# File 'lib/rspec/matchers/dsl.rb', line 235 def (&definition) define_user_override(__method__, definition) end |
#match(options = {}) {|Object| ... } ⇒Object
存储用于确定该匹配器是否通过或失败的代码块。代码块应返回布尔值。当匹配器传递给 expect(...).to
并且代码块返回 true
时,则期望通过。类似地,当匹配器传递给 expect(...).not_to
并且代码块返回 false
时,则期望通过。
默认情况下,匹配代码块将吞并期望错误(例如由使用期望(例如 expect(1).to eq 2
)引起的),如果您希望允许这些错误冒泡,请传递选项 :notify_expectation_failures => true
。
131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/rspec/matchers/dsl.rb', line 131 def match(={}, &match_block) define_user_override(:matches?, match_block) do |actual| @actual = actual RSpec::Support.with_failure_notifier(RAISE_NOTIFIER) do begin super(*actual_arg_for(match_block)) rescue RSpec::Expectations::ExpectationNotMetError raise if [:notify_expectation_failures] false end end end end |
#match_unless_raises(expected_exception = Exception) {|Object| ... } ⇒Object
当代码块引发异常而不是返回 false 以指示失败时,请使用它代替 match
。
188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/rspec/matchers/dsl.rb', line 188 def match_unless_raises(expected_exception=Exception, &match_block) define_user_override(:matches?, match_block) do |actual| @actual = actual begin super(*actual_arg_for(match_block)) rescue expected_exception => @rescued_exception false else true end end end |
#match_when_negated(options = {}) {|Object| ... } ⇒Object
当正向和负向形式需要不同的处理时,使用它来定义负向期望 (expect(...).not_to
) 的代码块。这很少需要,但可能很有用,例如在指定需要不同超时时间的异步进程时。
默认情况下,匹配代码块将吞并期望错误(例如由使用期望(例如 expect(1).to eq 2
)引起的),如果您希望允许这些错误冒泡,请传递选项 :notify_expectation_failures => true
。
160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/rspec/matchers/dsl.rb', line 160 def match_when_negated(={}, &match_block) define_user_override(:does_not_match?, match_block) do |actual| begin @actual = actual RSpec::Support.with_failure_notifier(RAISE_NOTIFIER) do super(*actual_arg_for(match_block)) end rescue RSpec::Expectations::ExpectationNotMetError raise if [:notify_expectation_failures] false end end end |
#supports_block_expectations ⇒Object
声明该匹配器可以在代码块期望中使用。在未声明此方法的情况下,用户将无法在代码块期望中使用您的匹配器。(例如 expect { do_something }.to matcher
)。
267 268 269 |
# File 'lib/rspec/matchers/dsl.rb', line 267 def supports_block_expectations define_method(:supports_block_expectations?) { true } end |