Creates a new mock with a name (that will be used in error messages only) == Options:
:null_object - if true, the mock object acts as a forgiving null object allowing any message to be sent to it.
# File lib/spec/mocks/mock.rb, line 10
10: def initialize(name=nil, stubs_and_options={})
11: if name.is_a?(Hash) && stubs_and_options.empty?
12: stubs_and_options = name
13: @name = nil
14: else
15: @name = name
16: end
17: @options = extract_options(stubs_and_options)
18: assign_stubs(stubs_and_options)
19: end
This allows for comparing the mock to other objects that proxy such as ActiveRecords belongs_to proxy objects. By making the other object run the comparison, we’re sure the call gets delegated to the proxy target.
# File lib/spec/mocks/mock.rb, line 25
25: def ==(other)
26: other == __mock_proxy
27: end
# File lib/spec/mocks/mock.rb, line 64
64: def assign_stubs(stubs)
65: stubs.each_pair do |message, response|
66: stub!(message).and_return(response)
67: end
68: end
# File lib/spec/mocks/mock.rb, line 56
56: def extract_option(source, target, key, default=nil)
57: if source[key]
58: target[key] = source.delete(key)
59: elsif default
60: target[key] = default
61: end
62: end
# File lib/spec/mocks/mock.rb, line 49
49: def extract_options(stubs_and_options)
50: options = {}
51: extract_option(stubs_and_options, options, :null_object)
52: extract_option(stubs_and_options, options, :__declared_as, 'Mock')
53: options
54: end
# File lib/spec/mocks/mock.rb, line 39
39: def method_missing(sym, *args, &block)
40: __mock_proxy.record_message_received(sym, args, block)
41: begin
42: return self if __mock_proxy.null_object?
43: super(sym, *args, &block)
44: rescue NameError
45: __mock_proxy.raise_unexpected_message_error sym, *args
46: end
47: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.