changelog shortlog tags changeset manifest revisions annotate raw

vendor/plugins/rspec/lib/spec/mocks/mock.rb

changeset 15: 64acf98d15f4
author: moriq@moriq.com
date: Mon Mar 10 10:12:58 2008 +0900 (16 years ago)
permissions: -rw-r--r--
description: add plugins rspec
1module Spec
2 module Mocks
3 class Mock
4 include Methods
5
6 # Creates a new mock with a +name+ (that will be used in error messages only)
7 # == Options:
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={})
10 @name = name
11 @options = parse_options(stubs_and_options)
12 assign_stubs(stubs_and_options)
13 end
14
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
19 def ==(other)
20 other == __mock_proxy
21 end
22
23 def method_missing(sym, *args, &block)
24 __mock_proxy.instance_eval {@messages_received << [sym, args, block]}
25 begin
26 return self if __mock_proxy.null_object?
27 super(sym, *args, &block)
28 rescue NameError
29 __mock_proxy.raise_unexpected_message_error sym, *args
30 end
31 end
32
33 def inspect
34 "#<#{self.class}:#{sprintf '0x%x', self.object_id} @name=#{@name.inspect}>"
35 end
36
37 private
38
39 def parse_options(options)
40 options.has_key?(:null_object) ? {:null_object => options.delete(:null_object)} : {}
41 end
42
43 def assign_stubs(stubs)
44 stubs.each_pair do |message, response|
45 stub!(message).and_return(response)
46 end
47 end
48 end
49 end
50end