类: RSpec::Matchers::DSL::Matcher

继承
Object
  • Object
显示全部
扩展自
Macros, RSpec::Matchers::DSL::Macros::Deprecated
包含
RSpec::Matchers, Composable, DefaultImplementations
定义于
lib/rspec/matchers/dsl.rb

概述

用于自定义匹配器的类。传递给 RSpec::Matchers.define 的代码块将在实例的单例类上下文中执行,并将使用 Macros 方法。

实例属性摘要 收起

实例方法摘要 收起

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

实例属性详情

#actualObject (只读)

公开正在匹配的值 - 通常是 expect 包装的对象对象。

449
450
451
# File 'lib/rspec/matchers/dsl.rb', line 449
def actual
  @actual
end

#block_argObject (只读)

预期中使用的块参数

456
457
458
# File 'lib/rspec/matchers/dsl.rb', line 456
def block_arg
  @block_arg
end

#expected_as_arrayObject (只读)

将预期值作为数组返回。这主要用于帮助从 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

#nameObject (只读)

匹配器的名称。

459
460
461
# File 'lib/rspec/matchers/dsl.rb', line 459
def name
  @name
end

#rescued_exceptionObject (只读)

公开在 match_unless_raises 中匹配期间引发的异常。这可能有助于提取用于失败消息的详细信息。

453
454
455
# File 'lib/rspec/matchers/dsl.rb', line 453
def rescued_exception
  @rescued_exception
end

实例方法详情

#expectedObject

提供预期值。如果向匹配器传递了多个参数,则将返回一个数组;否则将返回单个值。

另请参见

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

#inspectObject

添加名称(而不是神秘的十六进制数字),以便我们可以在错误消息(例如,对于 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