模块:RSpec::Mocks

定义于
lib/rspec/mocks.rb,
lib/rspec/mocks/proxy.rb,
lib/rspec/mocks/space.rb,
lib/rspec/mocks/syntax.rb,
lib/rspec/mocks/targets.rb,
lib/rspec/mocks/version.rb,
lib/rspec/mocks/order_group.rb,
lib/rspec/mocks/test_double.rb,
lib/rspec/mocks/mutate_const.rb,
lib/rspec/mocks/configuration.rb,
lib/rspec/mocks/message_chain.rb,
lib/rspec/mocks/method_double.rb,
lib/rspec/mocks/error_generator.rb,
lib/rspec/mocks/example_methods.rb,
lib/rspec/mocks/verifying_proxy.rb,
lib/rspec/mocks/matchers/receive.rb,
lib/rspec/mocks/method_reference.rb,
lib/rspec/mocks/object_reference.rb,
lib/rspec/mocks/verifying_double.rb,
lib/rspec/mocks/argument_matchers.rb,
lib/rspec/mocks/marshal_extension.rb,
lib/rspec/mocks/any_instance/chain.rb,
lib/rspec/mocks/any_instance/proxy.rb,
lib/rspec/mocks/message_expectation.rb,
lib/rspec/mocks/minitest_integration.rb,
lib/rspec/mocks/minitest_integration.rb,
lib/rspec/mocks/any_instance/recorder.rb,
lib/rspec/mocks/argument_list_matcher.rb,
lib/rspec/mocks/matchers/have_received.rb,
lib/rspec/mocks/any_instance/stub_chain.rb,
lib/rspec/mocks/instance_method_stasher.rb,
lib/rspec/mocks/matchers/receive_messages.rb,
lib/rspec/mocks/any_instance/message_chains.rb,
lib/rspec/mocks/any_instance/error_generator.rb,
lib/rspec/mocks/any_instance/stub_chain_chain.rb,
lib/rspec/mocks/verifying_message_expectation.rb,
lib/rspec/mocks/any_instance/expectation_chain.rb,
lib/rspec/mocks/matchers/receive_message_chain.rb,
lib/rspec/mocks/any_instance/expect_chain_chain.rb,
lib/rspec/mocks/matchers/expectation_customization.rb

概述

包含顶层实用方法。虽然它包含一些公用方法,但这些方法通常不建议从测试或示例中调用。它们主要用于与测试框架(例如 rspec-core)集成。

定义于命名空间下

模块: ArgumentMatchers, ExampleMethods, Matchers, Syntax, TestDouble, Version 类: ArgumentListMatcher, Configuration, Constant, ConstantMutator, DirectObjectReference, Double, MessageExpectation, NamedObjectReference, VerifyingMessageExpectation

常量摘要 收起

MockExpectationError =

当消息期望未满足时抛出。

::Minitest::Assertion
ExpiredTestDoubleError =

当测试替身在被拆卸后(通常在 rspec-core 示例结束时)使用时抛出。

Class.new(MockExpectationError)
OutsideOfExampleError =

当替身或部分替身在每测试生命周期之外使用时抛出。

Class.new(StandardError)
MockExpectationAlreadyInvokedError =

当在已经调用过的消息期望上调用期望自定义方法(例如 withand_return)时抛出。

Class.new(Exception)
CannotSupportArgMutationsError =
已弃用。

我们不再抛出此错误,但此常量保留到 RSpec 4,以符合 SemVer 规范。

当 RSpec 由于对 RSpec 持有用于稍后比较的参数进行了外部修改而无法支持某些情况时抛出。

Class.new(StandardError)

类方法摘要 收起

类方法详情

.allow_message(subject, message, opts = {}) ⇒Object

subject 上添加一个允许(模拟)

示例

使用传递的块定义 barfoo 的实现

x = 0
RSpec::Mocks.allow_message(bar, :foo) { x += 1 }

参数

  • subject

    将添加消息的主题

  • message

    一个符号,表示将添加的消息。

  • opts (默认值:{})

    一个选项哈希,:expected_from 用于设置原始调用位置

产量

  • 允许的可选实现

69
70
71
# File 'lib/rspec/mocks.rb', line 69
def self.allow_message(subject, message, opts={}, &block)
  space.proxy_for(subject).add_stub(message, opts, &block)
end

.configurationObject

模拟特定配置,与 RSpec.configuration(核心 RSpec 配置)区分开来。

206
207
208
# File 'lib/rspec/mocks/configuration.rb', line 206
def self.configuration
  @configuration ||= Configuration.new
end

.expect_message(subject, message, opts = {}) ⇒Object

subject 上设置消息期望。

示例

期望消息 foo 接收 bar,然后调用它

RSpec::Mocks.expect_message(bar, :foo)
bar.foo

参数

  • subject

    将期望消息的主题

  • message

    一个符号,表示将期望的消息。

  • opts (默认值:{})

    一个选项哈希,:expected_from 用于设置原始调用位置

产量

  • 期望的可选实现

84
85
86
# File 'lib/rspec/mocks.rb', line 84
def self.expect_message(subject, message, opts={}, &block)
  space.proxy_for(subject).add_message_expectation(message, opts, &block)
end

.setupObject

执行每测试/示例设置。这应该在测试或示例开始之前调用。

38
39
40
# File 'lib/rspec/mocks.rb', line 38
def self.setup
  @space_stack << (@space = space.new_scope)
end

.teardownObject

清理所有测试替身状态(包括在部分替身上重新定义的任何方法)。这必须在每个示例之后调用,即使在示例期间抛出了错误。

51
52
53
54
55
# File 'lib/rspec/mocks.rb', line 51
def self.teardown
  space.reset_all
  @space_stack.pop
  @space = @space_stack.last || @root_space
end

.verifyObject

验证在测试或示例期间设置的任何消息期望。这应该在示例结束时调用。

44
45
46
# File 'lib/rspec/mocks.rb', line 44
def self.verify
  space.verify_all
end

.with_temporary_scopeObject

调用传递的块,并在块执行后验证模拟。这允许在任意位置(例如 before(:all) 钩子)使用模拟。

返回

  • (Object)

    块的返回值

92
93
94
95
96
97
98
99
100
101
102
# File 'lib/rspec/mocks.rb', line 92
def self.with_temporary_scope
  setup
  begin
    result = yield
    verify
    result
  ensure
    teardown
  end
end