module RSpec::Mocks::Methods
def __mock_proxy
def __mock_proxy @mock_proxy ||= begin mp = if TestDouble === self Proxy.new(self, @name, @options) else Proxy.new(self) end Serialization.fix_for(self) mp end end
def __remove_mock_proxy
def __remove_mock_proxy @mock_proxy = nil end
def as_null_object
are declared, they'll work as expected. If not, the receiver is
Tells the object to respond to all messages. If specific stub values
def as_null_object @_null_object = true __mock_proxy.as_null_object end
def format_chain(*chain, &blk)
def format_chain(*chain, &blk) if Hash === chain.last hash = chain.pop hash.each do |k,v| chain << k blk = lambda { v } end end return chain.join('.').split('.'), blk end
def null_object?
def null_object? defined?(@_null_object) end
def received_message?(message, *args, &block)
- Private: -
def received_message?(message, *args, &block) __mock_proxy.received_message?(message, *args, &block) end
def rspec_reset
- Private: -
def rspec_reset __mock_proxy.reset end
def rspec_verify
- Private: -
def rspec_verify __mock_proxy.verify end
def should_not_receive(message, &block)
Sets and expectation that this object should _not_ receive a message
def should_not_receive(message, &block) __mock_proxy.add_negative_message_expectation(caller(1)[0], message.to_sym, &block) end
def should_receive(message, opts={}, &block)
logger.should_receive(:log)
thing_that_logs = ThingThatLogs.new(logger)
logger = double('logger')
@example
the end of the example.
Sets and expectation that this object should receive a message before
def should_receive(message, opts={}, &block) __mock_proxy.add_message_expectation(opts[:expected_from] || caller(1)[0], message.to_sym, opts, &block) end
def stub(message_or_hash, opts={}, &block)
counter.stub(:count => 37)
counter.stub(:count).and_return(37)
@example
Tells the object to respond to the message with the specified value.
def stub(message_or_hash, opts={}, &block) if Hash === message_or_hash message_or_hash.each {|message, value| stub(message).and_return value } else __mock_proxy.add_stub(caller(1)[0], message_or_hash.to_sym, opts, &block) end end
def stub_chain(*chain, &blk)
-
stub_chain(method1, method_to_value_hash)
-
stub_chain("method1.method2")
-
stub_chain(method1, method2)
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 = Mock.new stub(chain.shift) { next_in_chain } next_in_chain.stub_chain(*chain, &blk) end else stub(chain.shift, &blk) end end
def unstub(message)
shared `before` hook for the common case, but you want to replace it
This is rarely used, but can be useful when a stub is set up during a
restored.
`message`. On a real object, the original method (if it exists) is
Removes a stub. On a double, the object will no longer respond to
def unstub(message) __mock_proxy.remove_stub(message) end