模块:RSpec::Mocks::ExampleMethods

包含
ArgumentMatchers
定义于
lib/rspec/mocks/example_methods.rb

概述

包含在代码示例中使用的意图方法。将此混合到您的测试环境(例如测试框架基类)中,以便在您的测试框架中使用 rspec-mocks。如果您使用的是 rspec-core,它会为您处理这件事。

定义在命名空间下

模块: ExpectHost

实例方法摘要 折叠

ArgumentMatchers 中包含的方法

#any_args, #anything, #array_excluding, #array_including, #boolean, #duck_type, #hash_excluding, #hash_including, #instance_of, #kind_of, #no_args

实例方法详情

#allowObject

注意

如果您禁用 :expect 语法,此方法将被取消定义。

用于包装一个对象,以便为其上的方法设置存根。

示例

allow(dbl).to receive(:foo).with(5).and_return(:return_value)

    
# File 'lib/rspec/mocks/example_methods.rb', line 309

#allow_any_instance_ofObject

注意

这只有在您启用了 expect 语法时才可用。

用于包装一个类,以便为其实例上的方法设置存根。

示例

allow_any_instance_of(MyClass).to receive(:foo)

    
# File 'lib/rspec/mocks/example_methods.rb', line 327

#allow_message_expectations_on_nilObject

已弃用。

禁用有关对 nil 设置期望的警告消息。

默认情况下,当对 nil 设置期望时,会发出警告消息。这是为了防止出现假阳性,并在早期发现潜在的错误。

201
202
203
# File 'lib/rspec/mocks/example_methods.rb', line 201
def allow_message_expectations_on_nil
  RSpec::Mocks.space.proxy_for(nil).warn_about_expectations = false
end

#class_double(doubled_class) ⇒ Object #class_double(doubled_class, name) ⇒ Object #class_double(doubled_class, stubs) ⇒ Object #class_double(doubled_class, name, stubs) ⇒ Object

针对特定类构造一个测试替身。如果给定的类名已加载,则只允许对类上定义的类方法进行存根。在所有其他方面,它的行为类似于 double

重载

  • #class_double(doubled_class) ⇒Object

    参数

    • doubled_class (String, Module)
  • #class_double(doubled_class, name) ⇒Object

    参数

    • doubled_class (String, Module)
    • name (String/Symbol)

      用于在失败消息中使用的名称或描述

  • #class_double(doubled_class, stubs) ⇒Object

    参数

    • doubled_class (String, Module)
    • stubs (Hash)

      消息/返回值对的哈希

  • #class_double(doubled_class, name, stubs) ⇒Object

    参数

    • doubled_class (String, Module)
    • name (String/Symbol)

      用于在失败消息中使用的名称或描述

    • stubs (Hash)

      消息/返回值对的哈希

返回值

  • ClassVerifyingDouble

79
80
81
82
# File 'lib/rspec/mocks/example_methods.rb', line 79
def class_double(doubled_class, *args)
  ref = ObjectReference.for(doubled_class)
  ExampleMethods.declare_verifying_double(ClassVerifyingDouble, ref, *args)
end

#class_spy(doubled_class) ⇒ Object #class_spy(doubled_class, name) ⇒ Object #class_spy(doubled_class, stubs) ⇒ Object #class_spy(doubled_class, name, stubs) ⇒ Object

构造一个针对特定类的测试替身,该替身针对使用 have_received 进行了优化。如果给定的类名已加载,则只允许对类上定义的类方法进行存根。使用普通的替身,人们必须存根方法才能监视它们。class_spy 自动监视类响应的所有类方法。

重载

  • #class_spy(doubled_class) ⇒Object

    参数

    • doubled_class (String, Module)
  • #class_spy(doubled_class, name) ⇒Object

    参数

    • doubled_class (String, Class)
    • name (String/Symbol)

      用于在失败消息中使用的名称或描述

  • #class_spy(doubled_class, stubs) ⇒Object

    参数

    • doubled_class (String, Module)
    • stubs (Hash)

      消息/返回值对的哈希

  • #class_spy(doubled_class, name, stubs) ⇒Object

    参数

    • doubled_class (String, Class)
    • name (String/Symbol)

      用于在失败消息中使用的名称或描述

    • stubs (Hash)

      消息/返回值对的哈希

返回值

  • ClassVerifyingDouble

191
192
193
# File 'lib/rspec/mocks/example_methods.rb', line 191
def class_spy(*args)
  class_double(*args).as_null_object
end

#doubleDouble #double(name) ⇒ Double #double(stubs) ⇒ Double #double(name, stubs) ⇒ Double

构造 RSpec::Mocks::Double 的实例,使用可选名称进行配置,用于在失败消息中进行报告,以及可选的消息/返回值对哈希。

示例

book = double("book", :title => "The RSpec Book")
book.title #=> "The RSpec Book"

card = double("card", :suit => "Spades", :rank => "A")
card.suit  #=> "Spades"
card.rank  #=> "A"

重载

  • #double(name) ⇒Double

    参数

    • name (String/Symbol)

      用于在失败消息中使用的名称或描述

  • #double(stubs) ⇒Double

    参数

    • stubs (Hash)

      消息/返回值对的哈希

  • #double(name, stubs) ⇒Double

    参数

    • name (String/Symbol)

      用于在失败消息中使用的名称或描述

    • stubs (Hash)

      消息/返回值对的哈希

返回值

34
35
36
# File 'lib/rspec/mocks/example_methods.rb', line 34
def double(*args)
  ExampleMethods.declare_double(Double, *args)
end

#expectObject

注意

此方法通常由 rspec-expectations 提供。但是,如果您在没有 rspec-expectations 的情况下使用 rspec-mocks,这里有一个可用的定义。如果您禁用 :expect 语法,此方法将被取消定义。

用于在对对象设置模拟期望之前对其进行包装。

示例

expect(obj).to receive(:foo).with(5).and_return(:return_value)

    
# File 'lib/rspec/mocks/example_methods.rb', line 297

#expect_any_instance_ofObject

注意

如果您禁用 :expect 语法,此方法将被取消定义。

用于在对类的实例设置模拟期望之前对其进行包装。

示例

expect_any_instance_of(MyClass).to receive(:foo)

    
# File 'lib/rspec/mocks/example_methods.rb', line 318

#have_received(method_name, &block) ⇒Object

注意

当传递的参数在间谍记录接收到的消息后发生变异时,have_received(...).with(...) 无法正常工作。

验证给定对象在测试过程中是否接收到了预期消息。在间谍对象或空对象模拟上,这适用于任何方法,在其他对象上,该方法必须事先被存根才能验证消息。

以这种方式存根和验证接收到的消息实现了测试间谍模式。

示例

invitation = double('invitation', accept: true)
user.accept_invitation(invitation)
expect(invitation).to have_received(:accept)
# You can also use most message expectations:
expect(invitation).to have_received(:accept).with(mailer).once

参数

  • method_name (Symbol)

    预期被调用的方法的名称。

281
282
283
# File 'lib/rspec/mocks/example_methods.rb', line 281
def have_received(method_name, &block)
  Matchers::HaveReceived.new(method_name, &block)
end

#hide_const(constant_name) ⇒Object

隐藏具有给定值的命名常量。该常量将在测试期间为 undefined。

与方法存根一样,常量将在示例完成时恢复到其原始值。

示例

hide_const("MyClass") # => MyClass is now an undefined constant

参数

  • constant_name (String)

    常量的完全限定名称。不会考虑调用时当前常量范围。

256
257
258
# File 'lib/rspec/mocks/example_methods.rb', line 256
def hide_const(constant_name)
  ConstantMutator.hide(constant_name)
end

#instance_double(doubled_class) ⇒ Object #instance_double(doubled_class, name) ⇒ Object #instance_double(doubled_class, stubs) ⇒ Object #instance_double(doubled_class, name, stubs) ⇒ Object

构造一个针对特定类的测试模拟。如果给定的类名已被加载,则仅允许存根在该类上定义的实例方法。在所有其他方面,它的行为类似于 double

重载

  • #instance_double(doubled_class) ⇒Object

    参数

    • doubled_class (String, Class)
  • #instance_double(doubled_class, name) ⇒Object

    参数

    • doubled_class (String, Class)
    • name (String/Symbol)

      用于在失败消息中使用的名称或描述

  • #instance_double(doubled_class, stubs) ⇒Object

    参数

    • doubled_class (String, Class)
    • stubs (Hash)

      消息/返回值对的哈希

  • #instance_double(doubled_class, name, stubs) ⇒Object

    参数

    • doubled_class (String, Class)
    • name (String/Symbol)

      用于在失败消息中使用的名称或描述

    • stubs (Hash)

      消息/返回值对的哈希

返回值

  • InstanceVerifyingDouble

56
57
58
59
# File 'lib/rspec/mocks/example_methods.rb', line 56
def instance_double(doubled_class, *args)
  ref = ObjectReference.for(doubled_class)
  ExampleMethods.declare_verifying_double(InstanceVerifyingDouble, ref, *args)
end

#instance_spy(doubled_class) ⇒ Object #instance_spy(doubled_class, name) ⇒ Object #instance_spy(doubled_class, stubs) ⇒ Object #instance_spy(doubled_class, name, stubs) ⇒ Object

构造一个针对特定类的测试模拟,该模拟针对 have_received 进行了优化。如果给定的类名已被加载,则仅允许存根在该类上定义的实例方法。对于正常的模拟,必须存根方法才能对其进行间谍。instance_spy 会自动间谍所有该类响应的实例方法。

重载

  • #instance_spy(doubled_class) ⇒Object

    参数

    • doubled_class (String, Class)
  • #instance_spy(doubled_class, name) ⇒Object

    参数

    • doubled_class (String, Class)
    • name (String/Symbol)

      用于在失败消息中使用的名称或描述

  • #instance_spy(doubled_class, stubs) ⇒Object

    参数

    • doubled_class (String, Class)
    • stubs (Hash)

      消息/返回值对的哈希

  • #instance_spy(doubled_class, name, stubs) ⇒Object

    参数

    • doubled_class (String, Class)
    • name (String/Symbol)

      用于在失败消息中使用的名称或描述

    • stubs (Hash)

      消息/返回值对的哈希

返回值

  • InstanceVerifyingDouble

144
145
146
# File 'lib/rspec/mocks/example_methods.rb', line 144
def instance_spy(*args)
  instance_double(*args).as_null_object
end

#object_double(object_or_name) ⇒ Object #object_double(object_or_name, name) ⇒ Object #object_double(object_or_name, stubs) ⇒ Object #object_double(object_or_name, name, stubs) ⇒ Object

构造一个针对特定对象的测试模拟。仅允许存根对象响应的方法。如果提供字符串参数,则假定它引用一个用于验证的常量对象。在所有其他方面,它的行为类似于 double

重载

  • #object_double(object_or_name) ⇒Object

    参数

    • object_or_name (String, Object)
  • #object_double(object_or_name, name) ⇒Object

    参数

    • object_or_name (String, Object)
    • name (String/Symbol)

      用于在失败消息中使用的名称或描述

  • #object_double(object_or_name, stubs) ⇒Object

    参数

    • object_or_name (String, Object)
    • stubs (Hash)

      消息/返回值对的哈希

  • #object_double(object_or_name, name, stubs) ⇒Object

    参数

    • object_or_name (String, Object)
    • name (String/Symbol)

      用于在失败消息中使用的名称或描述

    • stubs (Hash)

      消息/返回值对的哈希

返回值

  • ObjectVerifyingDouble

102
103
104
105
# File 'lib/rspec/mocks/example_methods.rb', line 102
def object_double(object_or_name, *args)
  ref = ObjectReference.for(object_or_name, :allow_direct_object_refs)
  ExampleMethods.declare_verifying_double(ObjectVerifyingDouble, ref, *args)
end

#object_spy(object_or_name) ⇒ Object #object_spy(object_or_name, name) ⇒ Object #object_spy(object_or_name, stubs) ⇒ Object #object_spy(object_or_name, name, stubs) ⇒ Object

构造一个针对特定对象的测试模拟,该模拟针对 have_received 进行了优化。仅允许存根对象上定义的实例方法。对于正常的模拟,必须存根方法才能对其进行间谍。object_spy 会自动间谍对象响应的所有方法。

重载

  • #object_spy(object_or_name) ⇒Object

    参数

    • object_or_name (String, Object)
  • #object_spy(object_or_name, name) ⇒Object

    参数

    • object_or_name (String, Class)
    • name (String/Symbol)

      用于在失败消息中使用的名称或描述

  • #object_spy(object_or_name, stubs) ⇒Object

    参数

    • object_or_name (String, Object)
    • stubs (Hash)

      消息/返回值对的哈希

  • #object_spy(object_or_name, name, stubs) ⇒Object

    参数

    • object_or_name (String, Class)
    • name (String/Symbol)

      用于在失败消息中使用的名称或描述

    • stubs (Hash)

      消息/返回值对的哈希

返回值

  • ObjectVerifyingDouble

167
168
169
# File 'lib/rspec/mocks/example_methods.rb', line 167
def object_spy(*args)
  object_double(*args).as_null_object
end

#receiveObject

注意

如果您禁用 :expect 语法,此方法将被取消定义。

用于指定您期望或允许对象接收的消息。receive 返回的对象支持与 should_receivestub 一直支持的相同流畅接口,允许您约束参数或次数,并配置对象如何响应消息。

示例

expect(obj).to receive(:hello).with("world").exactly(3).times

    
# File 'lib/rspec/mocks/example_methods.rb', line 336

#receive_message_chain(method1, method2) ⇒ Object #receive_message_chain("method1.method2") ⇒ Object #receive_message_chain(method1, method_to_value_hash) ⇒ Object

注意

如果您禁用 :expect 语法,此方法将被取消定义。

存根/模拟对象或测试模拟上的消息链。

警告

链可以任意长,这使得以暴力方式违反 Demeter 法则变得非常容易,因此您应该将 receive_message_chain 的任何使用视为代码异味。即使并非所有代码异味都表明存在实际问题(例如,流畅接口),receive_message_chain 仍然会导致脆弱的示例。例如,如果您在规范中写了 allow(foo).to receive_message_chain(:bar, :baz => 37),然后实现调用了 foo.baz.bar,存根将不起作用。

示例

allow(double).to receive_message_chain("foo.bar") { :baz }
allow(double).to receive_message_chain(:foo, :bar => :baz)
allow(double).to receive_message_chain(:foo, :bar) { :baz }
# Given any of ^^ these three forms ^^:
double.foo.bar # => :baz

# Common use in Rails/ActiveRecord:
allow(Article).to receive_message_chain("recent.published") { [Article.new] }

    
# File 'lib/rspec/mocks/example_methods.rb', line 361

#receive_messagesObject

注意

如果您禁用 :expect 语法,此方法将被取消定义。

用于设置消息及其返回值的简写语法,您期望或允许对象接收这些消息。该方法接受消息及其各自返回值的哈希表。与 receive 不同,您不能使用块或流畅接口应用进一步的自定义。

示例

allow(obj).to receive_messages(:speak => "Hello World")
allow(obj).to receive_messages(:speak => "Hello", :meow => "Meow")

    
# File 'lib/rspec/mocks/example_methods.rb', line 348

#spyDouble #spy(name) ⇒ Double #spy(stubs) ⇒ Double #spy(name, stubs) ⇒ Double

构造一个针对 have_received 进行了优化的测试模拟。对于正常的模拟,必须存根方法才能对其进行间谍。间谍会自动间谍所有方法。

重载

  • #spy(name) ⇒Double

    参数

    • name (String/Symbol)

      用于在失败消息中使用的名称或描述

  • #spy(stubs) ⇒Double

    参数

    • stubs (Hash)

      消息/返回值对的哈希

  • #spy(name, stubs) ⇒Double

    参数

    • name (String/Symbol)

      用于在失败消息中使用的名称或描述

    • stubs (Hash)

      消息/返回值对的哈希

返回值

120
121
122
# File 'lib/rspec/mocks/example_methods.rb', line 120
def spy(*args)
  double(*args).as_null_object
end

#stub_const(constant_name, value, options = {}) ⇒Object

使用给定值存根命名常量。与方法存根一样,常量将在示例完成时恢复到其原始状态(或不存在,如果它之前未定义)。

示例

stub_const("MyClass", Class.new) # => Replaces (or defines) MyClass with a new class object.
stub_const("SomeModel::PER_PAGE", 5) # => Sets SomeModel::PER_PAGE to 5.

class CardDeck
  SUITS = [:Spades, :Diamonds, :Clubs, :Hearts]
  NUM_CARDS = 52
end
stub_const("CardDeck", Class.new)
CardDeck::SUITS # => uninitialized constant error
CardDeck::NUM_CARDS # => uninitialized constant error

stub_const("CardDeck", Class.new, :transfer_nested_constants => true)
CardDeck::SUITS # => our suits array
CardDeck::NUM_CARDS # => 52

stub_const("CardDeck", Class.new, :transfer_nested_constants => [:SUITS])
CardDeck::SUITS # => our suits array
CardDeck::NUM_CARDS # => uninitialized constant error

参数

  • constant_name (String)

    常量的完全限定名称。不会考虑调用时当前常量范围。

  • value (Object)

    使常量引用的值。示例完成时,常量将恢复到其先前状态。

  • options (Hash) (defaults to: {})

    存根选项。

选项哈希表(选项):

  • :transfer_nested_constants (Boolean, Array<Symbol>)

    确定将哪些嵌套常量(如果有)从常量的原始值转移到常量的新的值。这仅在原始值和新的值都是模块(或类)时才有效。

返回值

  • (Object)

    常量的存根值

241
242
243
# File 'lib/rspec/mocks/example_methods.rb', line 241
def stub_const(constant_name, value, options={})
  ConstantMutator.stub(constant_name, value, options)
end

#without_partial_double_verificationObject

在块持续时间内关闭部分模拟的验证,这在运行时定义了方法并且您希望为它们定义存根但不想为整个运行套件关闭部分模拟的情况下很有用。(例如,rspec-rails 中的视图规范)。

289
290
291
292
293
294
295
# File 'lib/rspec/mocks/example_methods.rb', line 289
def without_partial_double_verification
  original_state = Mocks.configuration.temporarily_suppress_partial_double_verification
  Mocks.configuration.temporarily_suppress_partial_double_verification = true
  yield
ensure
  Mocks.configuration.temporarily_suppress_partial_double_verification = original_state
end