类: RSpec::Matchers::DSL::Matcher
- 继承
-
Object
- Object
- RSpec::Matchers::DSL::Matcher
- 定义于
- lib/rspec/matchers/dsl.rb
概述
用于自定义匹配器的类。传递给 RSpec::Matchers.define
的代码块将在实例的单例类上下文中执行,并将使用 Macros 方法。
实例属性摘要 收起
-
#actual ⇒ Object 只读
公开与之匹配的值 - 通常是
expect
包裹的对象对象。 -
#block_arg ⇒ Object 只读
期望中使用的代码块参数。
-
#expected_as_array ⇒ Object 只读
以数组形式返回预期值。
-
#name ⇒ Object 只读
匹配器的名称。
-
#rescued_exception ⇒ Object 只读
公开
match_unless_raises
匹配过程中引发的异常。
实例方法摘要 收起
-
#expected ⇒ Object
提供预期值。
-
#initialize(name, declarations, matcher_execution_context, *expected, &block_arg) ⇒ Matcher 构造函数 私有
Matcher 的新实例。
-
#inspect ⇒ Object
添加名称(而不是神秘的十六进制数字),以便我们可以在错误消息中识别匹配器的实例(例如,对于
NoMethodError
)。 -
#respond_to?(method, include_private = false) ⇒ Boolean
:nocov: 指示此匹配器也响应来自
@matcher_execution_context
的消息。 -
#respond_to_missing?(method, include_private = false) ⇒ Boolean
指示此匹配器也响应来自
@matcher_execution_context
的消息。
从 Macros 包含的方法
chain, description, diffable, failure_message, failure_message_when_negated, match, match_unless_raises, match_when_negated, supports_block_expectations
从 RSpec::Matchers::DSL::Macros::Deprecated 包含的方法
failure_message_for_should, failure_message_for_should_not, match_for_should, match_for_should_not
从 Composable 包含的方法
#===, #and, #description_of, #or, should_enumerate?, surface_descriptions_in, unreadable_io?, #values_match?
从 RSpec::Matchers 包含的方法
#aggregate_failures, alias_matcher, #all, #be, #be_a, #be_a_kind_of, #be_an_instance_of, #be_between, #be_falsey, #be_nil, #be_truthy, #be_within, #change, clear_generated_description, configuration, #contain_exactly, #cover, define, define_negated_matcher, #end_with, #eq, #eql, #equal, #exist, #expect, generated_description, #have_attributes, #include, #match, #match_array, #output, #raise_error, #respond_to, #satisfy, #start_with, #throw_symbol, #yield_control, #yield_successive_args, #yield_with_args, #yield_with_no_args
从 RSpec::Matchers::DSL 包含的方法
#alias_matcher, #define, #define_negated_matcher
从 DefaultImplementations 包含的方法
#description, #diffable?, #expects_call_stack_jump?, #supports_block_expectations?, #supports_value_expectations?
从 BuiltIn::BaseMatcher::DefaultFailureMessages 包含的方法
#failure_message, #failure_message_when_negated
构造函数详情
#initialize(name, declarations, matcher_execution_context, *expected, &block_arg) ⇒Matcher
此方法属于私有 API。 尽可能避免使用此方法,因为它可能在未来被移除或更改。
返回一个新的 Matcher 实例。
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 |
# File 'lib/rspec/matchers/dsl.rb', line 462 def initialize(name, declarations, matcher_execution_context, *expected, &block_arg) @name = name @actual = nil @expected_as_array = expected @matcher_execution_context = matcher_execution_context @chained_method_clauses = [] @block_arg = block_arg klass = class << self # See `Macros#define_user_override` above, for an explanation. include(@user_method_defs = Module.new) self end RSpec::Support::WithKeywordsWhenNeeded.class_exec(klass, *expected, &declarations) end |
动态方法处理
此类通过method_missing方法
#method_missing(method, *args, &block) ⇒Object (私有)
负责将未处理的消息转发到 @matcher_execution_context
(通常是当前运行的 RSpec::Core::Example
)。RSpec-rails 需要此功能来定义包装 Rails 测试辅助方法的匹配器,但它本身也是一个有用的功能。
532 533 534 535 536 537 538 |
# File 'lib/rspec/matchers/dsl.rb', line 532 def method_missing(method, *args, &block) if @matcher_execution_context.respond_to?(method) @matcher_execution_context.__send__ method, *args, &block else super(method, *args, &block) end end |
实例属性详情
#actual ⇒Object (只读)
公开正在匹配的值 - 通常是 expect
包装的对象对象。
449 450 451 |
# File 'lib/rspec/matchers/dsl.rb', line 449 def actual @actual end |
#block_arg ⇒Object (只读)
预期中使用的块参数
456 457 458 |
# File 'lib/rspec/matchers/dsl.rb', line 456 def block_arg @block_arg end |
#expected_as_array ⇒Object (只读)
将预期值作为数组返回。这主要用于帮助从 RSpec 2.x 升级,因为在 RSpec 2 中,expected
始终返回一个数组。
494 495 496 |
# File 'lib/rspec/matchers/dsl.rb', line 494 def expected_as_array @expected_as_array end |
#name ⇒Object (只读)
匹配器的名称。
459 460 461 |
# File 'lib/rspec/matchers/dsl.rb', line 459 def name @name end |
#rescued_exception ⇒Object (只读)
公开在 match_unless_raises
中匹配期间引发的异常。这可能有助于提取用于失败消息的详细信息。
453 454 455 |
# File 'lib/rspec/matchers/dsl.rb', line 453 def rescued_exception @rescued_exception end |
实例方法详情
#expected ⇒Object
提供预期值。如果向匹配器传递了多个参数,则将返回一个数组;否则将返回单个值。
482 483 484 485 486 487 488 |
# File 'lib/rspec/matchers/dsl.rb', line 482 def expected if expected_as_array.size == 1 expected_as_array[0] else expected_as_array end end |
#inspect ⇒Object
添加名称(而不是神秘的十六进制数字),以便我们可以在错误消息(例如,对于 NoMethodError
)中识别匹配器实例。
499 500 501 |
# File 'lib/rspec/matchers/dsl.rb', line 499 def inspect "#<#{self.class.name} #{name}>" end |
#respond_to?(method, include_private = false) ⇒布尔值
:nocov: 指示此匹配器也响应来自 @matcher_execution_context
的消息。
514 515 516 |
# File 'lib/rspec/matchers/dsl.rb', line 514 def respond_to?(method, include_private=false) super || @matcher_execution_context.respond_to?(method, include_private) end |
#respond_to_missing?(method, include_private = false) ⇒布尔值
指示此匹配器也响应来自 @matcher_execution_context
的消息。此外,还支持为这些方法获取方法对象。
507 508 509 |
# File 'lib/rspec/matchers/dsl.rb', line 507 def respond_to_missing?(method, include_private=false) super || @matcher_execution_context.respond_to?(method, include_private) end |