Module: RSpec::Mocks::Methods
- Included in:
- TestDouble
- Defined in:
- lib/rspec/mocks/methods.rb
Overview
Methods that are added to every object.
Instance Method Summary collapse
-
#as_null_object ⇒ Object
Tells the object to respond to all messages.
-
#null_object? ⇒ Boolean
Returns true if this object has received
as_null_object. -
#should_not_receive(message, &block) ⇒ Object
Sets and expectation that this object should not receive a message during this example.
-
#should_receive(message, opts = {}, &block) ⇒ Object
Sets and expectation that this object should receive a message before the end of the example.
-
#stub(message_or_hash, opts = {}, &block) ⇒ Object
(also: #stub!)
Tells the object to respond to the message with the specified value.
-
#stub_chain(*chain, &blk) ⇒ Object
Stubs a chain of methods.
-
#unstub(message) ⇒ Object
(also: #unstub!)
Removes a stub.
Instance Method Details
#as_null_object ⇒ Object
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.
99 100 101 102 |
# File 'lib/rspec/mocks/methods.rb', line 99 def as_null_object @_null_object = true __mock_proxy.as_null_object end |
#null_object? ⇒ Boolean
Returns true if this object has received as_null_object
105 106 107 |
# File 'lib/rspec/mocks/methods.rb', line 105 def null_object? defined?(@_null_object) end |
#should_not_receive(message, &block) ⇒ Object
Sets and expectation that this object should not receive a message during this example.
20 21 22 |
# File 'lib/rspec/mocks/methods.rb', line 20 def should_not_receive(, &block) __mock_proxy.(caller(1)[0], .to_sym, &block) end |
#should_receive(message, opts = {}, &block) ⇒ Object
Sets and expectation that this object should receive a message before the end of the example.
14 15 16 |
# File 'lib/rspec/mocks/methods.rb', line 14 def should_receive(, opts={}, &block) __mock_proxy.(opts[:expected_from] || caller(1)[0], .to_sym, opts, &block) end |
#stub(message_or_hash, opts = {}, &block) ⇒ Object Also known as: stub!
Tells the object to respond to the message with the specified value.
31 32 33 34 35 36 37 |
# File 'lib/rspec/mocks/methods.rb', line 31 def stub(, opts={}, &block) if Hash === .each {|, value| stub().and_return value } else __mock_proxy.add_stub(caller(1)[0], .to_sym, opts, &block) end end |
#stub_chain(method1, method2) ⇒ Object #stub_chain("method1.method2") ⇒ Object #stub_chain(method1, method_to_value_hash) ⇒ Object
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.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/rspec/mocks/methods.rb', line 80 def stub_chain(*chain, &blk) chain, blk = format_chain(*chain, &blk) if chain.length > 1 if matching_stub = __mock_proxy.__send__(:find_matching_method_stub, chain[0].to_sym) chain.shift matching_stub.invoke.stub_chain(*chain, &blk) else next_in_chain = Object.new stub(chain.shift) { next_in_chain } next_in_chain.stub_chain(*chain, &blk) end else stub(chain.shift, &blk) end end |
#unstub(message) ⇒ Object Also known as: unstub!
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.
46 47 48 |
# File 'lib/rspec/mocks/methods.rb', line 46 def unstub() __mock_proxy.remove_stub() end |