顶级命名空间

定义在命名空间下

模块: RSpec 类: BasicObject, Class

实例方法摘要 折叠

包含自 RSpec::Mocks::ExampleMethods 的方法

allow, allow_any_instance_of, allow_message_expectations_on_nil, class_double, class_spy, double, expect, expect_any_instance_of, have_received, hide_const, instance_double, instance_spy, object_double, object_spy, receive, receive_message_chain, receive_messages, spy, stub_const, without_partial_double_verification

包含自 RSpec::Mocks::ArgumentMatchers 的方法

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

实例方法详情

#as_null_objectObject

注意

这仅在启用 should 语法时可用。

告诉对象对所有消息做出响应。如果声明了特定的存根值,它们将按预期工作。如果没有,将返回接收者。


    
# File 'lib/rspec/mocks/syntax.rb', line 283

#null_object?Object

注意

这仅在启用 should 语法时可用。

如果此对象已收到 as_null_object,则返回 true。


    
# File 'lib/rspec/mocks/syntax.rb', line 290

#should_not_receiveObject

设置一个期望,即此对象在本次示例中不应收到消息。


    
# File 'lib/rspec/mocks/syntax.rb', line 225

#should_receiveObject

注意

这仅在启用 should 语法时可用。

设置一个期望,即此对象在本次示例结束之前应收到消息。

示例

logger = double('logger')
thing_that_logs = ThingThatLogs.new(logger)
logger.should_receive(:log)
thing_that_logs.do_something_that_logs_a_message

另请参见


    
# File 'lib/rspec/mocks/syntax.rb', line 212

#stubObject

注意

这仅在启用 should 语法时可用。

告诉对象使用指定的值来响应消息。

示例

counter.stub(:count).and_return(37)
counter.stub(:count => 37)
counter.stub(:count) { 37 }

另请参见


    
# File 'lib/rspec/mocks/syntax.rb', line 230

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

注意

这仅在启用 should 语法时可用。

对方法链进行存根。

警告

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

示例

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

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

另请参见


    
# File 'lib/rspec/mocks/syntax.rb', line 252

#unstubObject

注意

这仅在启用 should 语法时可用。

移除存根。在双重对象上,对象将不再响应 message。在真实对象上,将恢复原始方法(如果存在)。

这很少使用,但在共享的 before 钩子中为通用情况设置存根,而你希望为特殊情况替换存根时,它很有用。


    
# File 'lib/rspec/mocks/syntax.rb', line 241