Class Spec::Mocks::MockHandler
In: lib/spec/mocks/mock_handler.rb
Parent: Object

Methods

Constants

DEFAULT_OPTIONS = { :null_object => false, :auto_verify => true

Public Class methods

[Source]

    # File lib/spec/mocks/mock_handler.rb, line 4
 4:       def initialize(target, name, options={})
 5:         @target = target
 6:         @name = name
 7:         @error_generator = ErrorGenerator.new target, name
 8:         @expectation_ordering = OrderGroup.new @error_generator
 9:         @expectations = []
10:         @messages_received = []
11:         @stubs = []
12:         @proxied_methods = {}
13:         @options = options ? DEFAULT_OPTIONS.dup.merge(options) : DEFAULT_OPTIONS
14:       end

Public Instance methods

[Source]

    # File lib/spec/mocks/mock_handler.rb, line 43
43:       def __add expected_from, sym, block
44:         current_spec = Runner::Specification.current
45:         current_spec.after_teardown {verify} if current_spec && @options[:auto_verify]
46:         define_expected_method(sym)
47:       end

[Source]

    # File lib/spec/mocks/mock_handler.rb, line 25
25:       def add_message_expectation(expected_from, sym, opts={}, &block)
26:         __add expected_from, sym, block
27:         @expectations << MessageExpectation.new(@error_generator, @expectation_ordering, expected_from, sym, block_given? ? block : nil, 1, opts)
28:         @expectations.last
29:       end

[Source]

    # File lib/spec/mocks/mock_handler.rb, line 31
31:       def add_negative_message_expectation(expected_from, sym, &block)
32:         __add expected_from, sym, block
33:         @expectations << NegativeMessageExpectation.new(@error_generator, @expectation_ordering, expected_from, sym, block_given? ? block : nil)
34:         @expectations.last
35:       end

[Source]

    # File lib/spec/mocks/mock_handler.rb, line 37
37:       def add_stub(expected_from, sym)
38:         __add expected_from, sym, nil
39:         @stubs << MethodStub.new(@error_generator, @expectation_ordering, expected_from, sym, nil)
40:         @stubs.last
41:       end

[Source]

    # File lib/spec/mocks/mock_handler.rb, line 49
49:       def define_expected_method(sym)
50:         if @target.respond_to?(sym) && !@proxied_methods[sym]
51:           @proxied_methods[sym] = @target.method(sym)
52:         end
53: 
54:         metaclass_eval %-
55:           def #{sym}(*args, &block)
56:             __mock_handler.message_received :#{sym}, *args, &block
57:           end
58:         -
59:       end

[Source]

    # File lib/spec/mocks/mock_handler.rb, line 88
88:       def define_instance_method(method_name, method_obj)
89:         (class << @target; self; end).class_eval do
90:           define_method method_name, &method_obj
91:         end
92:       end

[Source]

     # File lib/spec/mocks/mock_handler.rb, line 119
119:       def find_almost_matching_expectation(sym, *args)
120:         @expectations.find {|expectation| expectation.matches_name_but_not_args(sym, args)}
121:       end

[Source]

     # File lib/spec/mocks/mock_handler.rb, line 115
115:       def find_matching_expectation(sym, *args)
116:         @expectations.find {|expectation| expectation.matches(sym, args)}
117:       end

[Source]

     # File lib/spec/mocks/mock_handler.rb, line 123
123:       def find_matching_method_stub(sym)
124:         @stubs.find {|stub| stub.matches(sym, [])}
125:       end

[Source]

     # File lib/spec/mocks/mock_handler.rb, line 127
127:       def has_negative_expectation?(sym)
128:         @expectations.detect {|expectation| expectation.negative_expectation_for?(sym)}
129:       end

[Source]

     # File lib/spec/mocks/mock_handler.rb, line 131
131:       def message_received(sym, *args, &block)
132:         if expectation = find_matching_expectation(sym, *args)
133:           expectation.invoke(args, block)
134:         elsif stub = find_matching_method_stub(sym)
135:           stub.invoke([], block)
136:         elsif expectation = find_almost_matching_expectation(sym, *args)
137:           raise_unexpected_message_args_error(expectation, *args) unless has_negative_expectation?(sym) unless null_object?
138:         else
139:           @target.send :method_missing, sym, *args, &block
140:         end
141:       end

[Source]

     # File lib/spec/mocks/mock_handler.rb, line 106
106:       def metaclass_eval(str)
107:         (class << @target; self; end).class_eval str
108:       end

[Source]

    # File lib/spec/mocks/mock_handler.rb, line 21
21:       def null_object?
22:         @options[:null_object]
23:       end

[Source]

     # File lib/spec/mocks/mock_handler.rb, line 143
143:       def raise_unexpected_message_args_error(expectation, *args)
144:         @error_generator.raise_unexpected_message_args_error expectation, *args
145:       end

[Source]

     # File lib/spec/mocks/mock_handler.rb, line 147
147:       def raise_unexpected_message_error(sym, *args)
148:         @error_generator.raise_unexpected_message_error sym, *args
149:       end

[Source]

     # File lib/spec/mocks/mock_handler.rb, line 110
110:       def received_message?(sym, *args, &block)
111:         return true if @messages_received.find {|array| array == [sym, args, block]}
112:         return false
113:       end

[Source]

    # File lib/spec/mocks/mock_handler.rb, line 69
69:       def reset
70:         clear_expectations
71:         clear_stubs
72:         reset_proxied_methods
73:         clear_proxied_methods
74:       end

[Source]

    # File lib/spec/mocks/mock_handler.rb, line 82
82:       def reset_proxied_methods
83:         @proxied_methods.each do |method_name, method_obj|
84:           define_instance_method(method_name, method_obj)
85:         end
86:       end

[Source]

    # File lib/spec/mocks/mock_handler.rb, line 76
76:       def verify_expectations
77:         @expectations.each do |expectation|
78:           expectation.verify_messages_received
79:         end
80:       end

[Validate]