Class: BasicObject

Defined in:
lib/rspec/mocks/syntax.rb

Overview

The legacy :should syntax adds the following methods directly to BasicObject so that they are available off of any object. Note, however, that this syntax does not always play nice with delegate/proxy objects. We recommend you use the non-monkeypatching :expect syntax instead.

See Also:

Instance Method Summary collapse

Instance Method Details

#as_null_objectObject

Note:

This is only available when you have enabled the should syntax.

Tells the object to respond to all messages. If specific stub values are declared, they'll work as expected. If not, the receiver is returned.



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

#null_object?Object

Note:

This is only available when you have enabled the should syntax.

Returns true if this object has received as_null_object



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

#should_not_receiveObject

Sets and expectation that this object should not receive a message during this example.



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

#should_receiveObject

Note:

This is only available when you have enabled the should syntax.

Sets an expectation that this object should receive a message before the end of the example.

Examples:


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

See Also:



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

#stubObject

Note:

This is only available when you have enabled the should syntax.

Tells the object to respond to the message with the specified value.

Examples:


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

See Also:



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

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

Note:

This is only available when you have enabled the should syntax.

Stubs a chain of methods.

Warning:

Chains can be arbitrarily long, which makes it quite painless to violate the Law of Demeter in violent ways, so you should consider any use of stub_chain a code smell. Even though not all code smells indicate real problems (think fluent interfaces), stub_chain still results in brittle examples. For example, if you write foo.stub_chain(:bar, :baz => 37) in a spec and then the implementation calls foo.baz.bar, the stub will not work.

Examples:


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] }

See Also:



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

#unstubObject

Note:

This is only available when you have enabled the should syntax.

Removes a stub. On a double, the object will no longer respond to message. On a real object, the original method (if it exists) is restored.

This is rarely used, but can be useful when a stub is set up during a shared before hook for the common case, but you want to replace it for a special case.



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