RSpec::Mocks::Methods

Methods that are added to every object.

Public Instance Methods

as_null_object() click to toggle source

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/methods.rb, line 99
 99:       def as_null_object
100:         @_null_object = true
101:         __mock_proxy.as_null_object
102:       end
null_object?() click to toggle source

Returns true if this object has received `as_null_object`

     # File lib/rspec/mocks/methods.rb, line 105
105:       def null_object?
106:         defined?(@_null_object)
107:       end
received_message?(message, *args, &block) click to toggle source

@private

     # File lib/rspec/mocks/methods.rb, line 110
110:       def received_message?(message, *args, &block)
111:         __mock_proxy.received_message?(message, *args, &block)
112:       end
rspec_reset() click to toggle source

@private

     # File lib/rspec/mocks/methods.rb, line 120
120:       def rspec_reset
121:         __mock_proxy.reset
122:       end
rspec_verify() click to toggle source

@private

     # File lib/rspec/mocks/methods.rb, line 115
115:       def rspec_verify
116:         __mock_proxy.verify
117:       end
should_not_receive(message, &block) click to toggle source

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

    # File lib/rspec/mocks/methods.rb, line 20
20:       def should_not_receive(message, &block)
21:         __mock_proxy.add_negative_message_expectation(caller(1)[0], message.to_sym, &block)
22:       end
should_receive(message, opts={}, &block) click to toggle source

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

@example

    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/methods.rb, line 14
14:       def should_receive(message, opts={}, &block)
15:         __mock_proxy.add_message_expectation(opts[:expected_from] || caller(1)[0], message.to_sym, opts, &block)
16:       end
stub(message_or_hash, opts={}, &block) click to toggle source

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

@example

    counter.stub(:count).and_return(37)
    counter.stub(:count => 37)
    counter.stub(:count) { 37 }
    # File lib/rspec/mocks/methods.rb, line 31
31:       def stub(message_or_hash, opts={}, &block)
32:         if Hash === message_or_hash
33:           message_or_hash.each {|message, value| stub(message).and_return value }
34:         else
35:           __mock_proxy.add_stub(caller(1)[0], message_or_hash.to_sym, opts, &block)
36:         end
37:       end
Also aliased as: stub!
stub!(message_or_hash, opts={}, &block) click to toggle source
Alias for: stub
stub_chain(*chain, &blk) click to toggle source

@overload stub_chain(method1, method2) @overload stub_chain(“method1.method2“) @overload stub_chain(method1, method_to_value_hash)

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.

@example

    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/methods.rb, line 80
80:       def stub_chain(*chain, &blk)
81:         chain, blk = format_chain(*chain, &blk)
82:         if chain.length > 1
83:           if matching_stub = __mock_proxy.__send__(:find_matching_method_stub, chain[0].to_sym)
84:             chain.shift
85:             matching_stub.invoke.stub_chain(*chain, &blk)
86:           else
87:             next_in_chain = Object.new
88:             stub(chain.shift) { next_in_chain }
89:             next_in_chain.stub_chain(*chain, &blk)
90:           end
91:         else
92:           stub(chain.shift, &blk)
93:         end
94:       end
unstub(message) click to toggle source

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/methods.rb, line 46
46:       def unstub(message)
47:         __mock_proxy.remove_stub(message)
48:       end
Also aliased as: unstub!
unstub!(message) click to toggle source
Alias for: unstub

Private Instance Methods

__mock_proxy() click to toggle source
     # File lib/rspec/mocks/methods.rb, line 126
126:       def __mock_proxy
127:         @mock_proxy ||= begin
128:           mp = if TestDouble === self
129:             Proxy.new(self, @name, @options)
130:           else
131:             Proxy.new(self)
132:           end
133: 
134:           Serialization.fix_for(self)
135:           mp
136:         end
137:       end
format_chain(*chain, &blk) click to toggle source
     # File lib/rspec/mocks/methods.rb, line 139
139:       def format_chain(*chain, &blk)
140:         if Hash === chain.last
141:           hash = chain.pop
142:           hash.each do |k,v|
143:             chain << k
144:             blk = lambda { v }
145:           end
146:         end
147:         return chain.join('.').split('.'), blk
148:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.