6 # Creates a new mock with a +name+ (that will be used in error messages only)
8 # * <tt>:null_object</tt> - if true, the mock object acts as a forgiving null object allowing any message to be sent to it.
9 def initialize(name, stubs_and_options={})
11 @options = parse_options(stubs_and_options)
12 assign_stubs(stubs_and_options)
15 # This allows for comparing the mock to other objects that proxy
16 # such as ActiveRecords belongs_to proxy objects
17 # By making the other object run the comparison, we're sure the call gets delegated to the proxy target
18 # This is an unfortunate side effect from ActiveRecord, but this should be safe unless the RHS redefines == in a nonsensical manner
23 def method_missing(sym, *args, &block)
24 __mock_proxy.instance_eval {@messages_received << [sym, args, block]}
26 return self if __mock_proxy.null_object?
27 super(sym, *args, &block)
29 __mock_proxy.raise_unexpected_message_error sym, *args
34 "#<#{self.class}:#{sprintf '0x%x', self.object_id} @name=#{@name.inspect}>"
39 def parse_options(options)
40 options.has_key?(:null_object) ? {:null_object => options.delete(:null_object)} : {}
43 def assign_stubs(stubs)
44 stubs.each_pair do |message, response|
45 stub!(message).and_return(response)