模块:RSpec::Mocks::ExampleMethods
- 定义于
- lib/rspec/mocks/example_methods.rb
概述
包含在代码示例中使用的意图方法。将此混合到您的测试环境(例如测试框架基类)中,以便在您的测试框架中使用 rspec-mocks。如果您使用的是 rspec-core,它会为您处理这件事。
定义在命名空间下
模块: ExpectHost
实例方法摘要 折叠
-
#allow ⇒ Object
用于包装一个对象,以便为其上的方法设置存根。
-
#allow_any_instance_of ⇒ Object
用于包装一个类,以便为其实例上的方法设置存根。
- #allow_message_expectations_on_nil ⇒ Object 已弃用 已弃用。
-
#class_double(doubled_class, *args) ⇒ Object
针对特定类构造一个测试替身。
-
#class_spy(*args) ⇒ Object
构造一个针对特定类的测试替身,该替身针对使用
have_received
进行了优化。 -
#double(*args) ⇒ Double
构造 RSpec::Mocks::Double 的实例,使用可选名称进行配置,用于在失败消息中进行报告,以及可选的消息/返回值对哈希。
-
#expect ⇒ Object
用于包装一个对象,以便为其设置模拟期望。
-
#expect_any_instance_of ⇒ Object
用于包装一个类,以便为其实例设置模拟期望。
-
#have_received(method_name, &block) ⇒ Object
验证给定对象在测试过程中是否收到了预期的消息。
-
#hide_const(constant_name) ⇒ Object
使用给定值隐藏命名常量。
-
#instance_double(doubled_class, *args) ⇒ Object
针对特定类构造一个测试替身。
-
#instance_spy(*args) ⇒ Object
构造一个针对特定类的测试替身,该替身针对使用
have_received
进行了优化。 -
#object_double(object_or_name, *args) ⇒ Object
针对特定对象构造一个测试替身。
-
#object_spy(*args) ⇒ Object
构造一个针对特定对象的测试替身,该替身针对使用
have_received
进行了优化。 -
#receive ⇒ Object
用于指定您期望或允许对象接收的消息。
-
#receive_message_chain ⇒ Object
存根/模拟对象或测试替身上的消息链。
-
#receive_messages ⇒ Object
简写语法,用于设置您期望或允许对象接收的消息及其返回值。
-
#spy(*args) ⇒ Double
构造一个针对使用
have_received
进行了优化的测试替身。 -
#stub_const(constant_name, value, options = {}) ⇒ Object
使用给定值存根命名常量。
-
#without_partial_double_verification ⇒ Object
在块的持续时间内关闭部分替身验证,这在运行时定义方法并且您希望为它们定义存根但不希望为整个运行套件关闭部分替身的情况下非常有用。
从 ArgumentMatchers 中包含的方法
#any_args, #anything, #array_excluding, #array_including, #boolean, #duck_type, #hash_excluding, #hash_including, #instance_of, #kind_of, #no_args
实例方法详情
#allow ⇒Object
如果您禁用 :expect
语法,此方法将被取消定义。
用于包装一个对象,以便为其上的方法设置存根。
# File 'lib/rspec/mocks/example_methods.rb', line 309
|
#allow_any_instance_of ⇒Object
这只有在您启用了 expect
语法时才可用。
用于包装一个类,以便为其实例上的方法设置存根。
# File 'lib/rspec/mocks/example_methods.rb', line 327
|
#allow_message_expectations_on_nil ⇒Object
禁用有关对 nil 设置期望的警告消息。
默认情况下,当对 nil 设置期望时,会发出警告消息。这是为了防止出现假阳性,并在早期发现潜在的错误。
201 202 203 |
# File 'lib/rspec/mocks/example_methods.rb', line 201 def 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。
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.(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 自动监视类响应的所有类方法。
191 192 193 |
# File 'lib/rspec/mocks/example_methods.rb', line 191 def class_spy(*args) class_double(*args).as_null_object end |
#double ⇒ Double #double(name) ⇒ Double #double(stubs) ⇒ Double #double(name, stubs) ⇒ Double
构造 RSpec::Mocks::Double 的实例,使用可选名称进行配置,用于在失败消息中进行报告,以及可选的消息/返回值对哈希。
34 35 36 |
# File 'lib/rspec/mocks/example_methods.rb', line 34 def double(*args) ExampleMethods.declare_double(Double, *args) end |
#expect ⇒Object
此方法通常由 rspec-expectations 提供。但是,如果您在没有 rspec-expectations 的情况下使用 rspec-mocks,这里有一个可用的定义。如果您禁用 :expect
语法,此方法将被取消定义。
用于在对对象设置模拟期望之前对其进行包装。
# File 'lib/rspec/mocks/example_methods.rb', line 297
|
#expect_any_instance_of ⇒Object
如果您禁用 :expect
语法,此方法将被取消定义。
用于在对类的实例设置模拟期望之前对其进行包装。
# File 'lib/rspec/mocks/example_methods.rb', line 318
|
#have_received(method_name, &block) ⇒Object
当传递的参数在间谍记录接收到的消息后发生变异时,have_received(...).with(...)
无法正常工作。
验证给定对象在测试过程中是否接收到了预期消息。在间谍对象或空对象模拟上,这适用于任何方法,在其他对象上,该方法必须事先被存根才能验证消息。
以这种方式存根和验证接收到的消息实现了测试间谍模式。
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。
与方法存根一样,常量将在示例完成时恢复到其原始值。
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。
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.(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
会自动间谍所有该类响应的实例方法。
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。
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.(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
会自动间谍对象响应的所有方法。
167 168 169 |
# File 'lib/rspec/mocks/example_methods.rb', line 167 def object_spy(*args) object_double(*args).as_null_object end |
#receive ⇒Object
如果您禁用 :expect
语法,此方法将被取消定义。
用于指定您期望或允许对象接收的消息。receive
返回的对象支持与 should_receive
和 stub
一直支持的相同流畅接口,允许您约束参数或次数,并配置对象如何响应消息。
# 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
,存根将不起作用。
# File 'lib/rspec/mocks/example_methods.rb', line 361
|
#receive_messages ⇒Object
如果您禁用 :expect
语法,此方法将被取消定义。
用于设置消息及其返回值的简写语法,您期望或允许对象接收这些消息。该方法接受消息及其各自返回值的哈希表。与 receive
不同,您不能使用块或流畅接口应用进一步的自定义。
# File 'lib/rspec/mocks/example_methods.rb', line 348
|
#spy ⇒ Double #spy(name) ⇒ Double #spy(stubs) ⇒ Double #spy(name, stubs) ⇒ Double
构造一个针对 have_received
进行了优化的测试模拟。对于正常的模拟,必须存根方法才能对其进行间谍。间谍会自动间谍所有方法。
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
使用给定值存根命名常量。与方法存根一样,常量将在示例完成时恢复到其原始状态(或不存在,如果它之前未定义)。
241 242 243 |
# File 'lib/rspec/mocks/example_methods.rb', line 241 def stub_const(constant_name, value, ={}) ConstantMutator.stub(constant_name, value, ) end |
#without_partial_double_verification ⇒Object
在块持续时间内关闭部分模拟的验证,这在运行时定义了方法并且您希望为它们定义存根但不想为整个运行套件关闭部分模拟的情况下很有用。(例如,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 |