public
Description: Behaviour Driven Development framework for Ruby
Homepage: http://rspec.info
Clone URL: git://github.com/dchelimsky/rspec.git
Click here to lend your support to: rspec and make a donation at www.pledgie.com !
rspec / lib / spec / mocks / any_instance / method_stubber.rb
100644 68 lines (54 sloc) 1.582 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
module Spec
  module Mocks
    module AnyInstance
      class MethodStubber
 
        def initialize(target, message)
          @target = target
          @message = message
          @munged_sym = "__rspec_proxy_any_instance_#{message}".to_sym
        end
 
        def add_stub
          add_stub_with_value nil
        end
 
        def and_return(value)
          add_stub_with_value value
        end
 
        def and_raise(*raise_params)
          add_stub_with_error *raise_params
        end
 
        def reset!
          swap_methods @munged_sym, @message
          @target.send(:remove_method, @munged_sym) if @target.method_defined?(@munged_sym)
        end
        alias_method :rspec_reset, :reset!
        
        def verified?
          @verified
        end
        
        def rspec_verify
          @verified = true
        end
        
        private
        def define_stub(&impl)
          store_current_instance_method do |message|
            @target.send(:define_method, message, &impl)
          end
        end
        
        def add_stub_with_value(value)
          define_stub { value }
        end
 
        def add_stub_with_error(*raise_params)
          define_stub { raise *raise_params }
        end
 
        def store_current_instance_method(&block)
          swap_methods @message, @munged_sym, &block
        end
 
        def swap_methods(first, second)
          @target.class_eval do
            alias_method second, first if method_defined?(first)
          end
          yield first if block_given?
        end
 
      end
    end
  end
end