| Class | Spec::Mocks::Proxy |
| In: |
lib/spec/mocks/proxy.rb
|
| Parent: | Object |
| DEFAULT_OPTIONS | = | { :null_object => false, } |
# File lib/spec/mocks/proxy.rb, line 8
8: def initialize(target, name, options={})
9: @target = target
10: @name = name
11: @error_generator = ErrorGenerator.new target, name
12: @expectation_ordering = OrderGroup.new @error_generator
13: @expectations = []
14: @messages_received = []
15: @stubs = []
16: @proxied_methods = []
17: @options = options ? DEFAULT_OPTIONS.dup.merge(options) : DEFAULT_OPTIONS
18: end
# File lib/spec/mocks/proxy.rb, line 24
24: def add_message_expectation(expected_from, sym, opts={}, &block)
25: __add sym, block
26: @expectations << MessageExpectation.new(@error_generator, @expectation_ordering, expected_from, sym, block_given? ? block : nil, 1, opts)
27: @expectations.last
28: end
# File lib/spec/mocks/proxy.rb, line 30
30: def add_negative_message_expectation(expected_from, sym, &block)
31: __add sym, block
32: @expectations << NegativeMessageExpectation.new(@error_generator, @expectation_ordering, expected_from, sym, block_given? ? block : nil)
33: @expectations.last
34: end
# File lib/spec/mocks/proxy.rb, line 36
36: def add_stub(expected_from, sym)
37: __add sym, nil
38: @stubs.unshift MethodStub.new(@error_generator, @expectation_ordering, expected_from, sym, nil)
39: @stubs.first
40: end
# File lib/spec/mocks/proxy.rb, line 62
62: def has_negative_expectation?(sym)
63: @expectations.detect {|expectation| expectation.negative_expectation_for?(sym)}
64: end
# File lib/spec/mocks/proxy.rb, line 66
66: def message_received(sym, *args, &block)
67: if expectation = find_matching_expectation(sym, *args)
68: expectation.invoke(args, block)
69: elsif stub = find_matching_method_stub(sym)
70: stub.invoke([], block)
71: elsif expectation = find_almost_matching_expectation(sym, *args)
72: raise_unexpected_message_args_error(expectation, *args) unless has_negative_expectation?(sym) unless null_object?
73: else
74: @target.send :method_missing, sym, *args, &block
75: end
76: end
# File lib/spec/mocks/proxy.rb, line 78
78: def raise_unexpected_message_args_error(expectation, *args)
79: @error_generator.raise_unexpected_message_args_error expectation, *args
80: end
# File lib/spec/mocks/proxy.rb, line 82
82: def raise_unexpected_message_error(sym, *args)
83: @error_generator.raise_unexpected_message_error sym, *args
84: end