Class: Spec::Mocks::Proxy
Constant Summary collapse
- DEFAULT_OPTIONS =
{ :null_object => false, }
- @@warn_about_expectations_on_nil =
true
Class Method Summary collapse
Instance Method Summary collapse
- #add_message_expectation(expected_from, sym, opts = {}, &block) ⇒ Object
- #add_negative_message_expectation(expected_from, sym, &block) ⇒ Object
- #add_stub(expected_from, sym, opts = {}, &implementation) ⇒ Object
- #as_null_object ⇒ Object
- #build_expectation(expected_from, sym, opts, &block) ⇒ Object
- #find_matching_method_stub(sym, *args) ⇒ Object
- #has_negative_expectation?(sym) ⇒ Boolean
-
#initialize(target, name = nil, options = {}) ⇒ Proxy
constructor
A new instance of Proxy.
- #invoke_expectation(expectation, *args, &block) ⇒ Object
- #message_received(sym, *args, &block) ⇒ Object
- #null_object? ⇒ Boolean
- #ok_to_invoke_stub?(stub, expectation) ⇒ Boolean
- #raise_unexpected_message_args_error(expectation, *args) ⇒ Object
- #raise_unexpected_message_error(sym, *args) ⇒ Object
- #received_message?(sym, *args, &block) ⇒ Boolean
- #record_almost_matching_expectation(expectation, sym, *args, &block) ⇒ Object
- #record_message_received(sym, args, block) ⇒ Object
- #record_stub(stub, sym, args, &block) ⇒ Object
- #remove_stub(message) ⇒ Object
- #reset ⇒ Object
-
#verify ⇒ Object
:nodoc:.
Constructor Details
#initialize(target, name = nil, options = {}) ⇒ Proxy
Returns a new instance of Proxy.
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/spec/mocks/proxy.rb', line 18 def initialize(target, name=nil, ={}) @target = target @name = name @error_generator = ErrorGenerator.new target, name, @expectation_ordering = OrderGroup.new @error_generator @expectations = [] @messages_received = [] @stubs = [] @proxied_methods = [] @options = ? DEFAULT_OPTIONS.dup.merge() : DEFAULT_OPTIONS @already_proxied_respond_to = false end |
Class Method Details
.allow_message_expectations_on_nil ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/spec/mocks/proxy.rb', line 10 def self. @@warn_about_expectations_on_nil = false # ensure nil.rspec_verify is called even if an expectation is not set in the example # otherwise the allowance would effect subsequent examples $rspec_mocks.add(nil) unless $rspec_mocks.nil? end |
Instance Method Details
#add_message_expectation(expected_from, sym, opts = {}, &block) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/spec/mocks/proxy.rb', line 40 def (expected_from, sym, opts={}, &block) __add sym warn_if_nil_class sym @expectations << build_expectation(expected_from, sym, opts, &block) @expectations.last end |
#add_negative_message_expectation(expected_from, sym, &block) ⇒ Object
55 56 57 58 59 60 |
# File 'lib/spec/mocks/proxy.rb', line 55 def (expected_from, sym, &block) __add sym warn_if_nil_class sym @expectations << NegativeMessageExpectation.new(@error_generator, @expectation_ordering, expected_from, sym, block_given? ? block : nil) @expectations.last end |
#add_stub(expected_from, sym, opts = {}, &implementation) ⇒ Object
62 63 64 65 66 |
# File 'lib/spec/mocks/proxy.rb', line 62 def add_stub(expected_from, sym, opts={}, &implementation) __add sym @stubs.unshift MessageExpectation.new(@error_generator, @expectation_ordering, expected_from, sym, nil, :any, opts, &implementation) @stubs.first end |
#as_null_object ⇒ Object
35 36 37 38 |
# File 'lib/spec/mocks/proxy.rb', line 35 def as_null_object @options[:null_object] = true @target end |
#build_expectation(expected_from, sym, opts, &block) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/spec/mocks/proxy.rb', line 47 def build_expectation(expected_from, sym, opts, &block) if stub = find_matching_method_stub(sym) stub.build_child(expected_from, block_given?? block : nil, 1, opts) else MessageExpectation.new(@error_generator, @expectation_ordering, expected_from, sym, block_given? ? block : nil, 1, opts) end end |
#find_matching_method_stub(sym, *args) ⇒ Object
151 152 153 |
# File 'lib/spec/mocks/proxy.rb', line 151 def find_matching_method_stub(sym, *args) @stubs.find {|stub| stub.matches(sym, args)} end |
#has_negative_expectation?(sym) ⇒ Boolean
96 97 98 |
# File 'lib/spec/mocks/proxy.rb', line 96 def has_negative_expectation?(sym) @expectations.any? {|expectation| expectation.negative_expectation_for?(sym)} end |
#invoke_expectation(expectation, *args, &block) ⇒ Object
128 129 130 |
# File 'lib/spec/mocks/proxy.rb', line 128 def invoke_expectation(expectation, *args, &block) expectation.invoke(*args, &block) end |
#message_received(sym, *args, &block) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/spec/mocks/proxy.rb', line 104 def (sym, *args, &block) expectation = find_matching_expectation(sym, *args) stub = find_matching_method_stub(sym, *args) if ok_to_invoke_stub?(stub, expectation) record_stub(stub, sym, args, &block) elsif expectation invoke_expectation(expectation, *args, &block) elsif expectation = find_almost_matching_expectation(sym, *args) record_almost_matching_expectation(expectation, sym, *args, &block) elsif @target.is_a?(Class) @target.superclass.send(sym, *args, &block) else @target.__send__ :method_missing, sym, *args, &block end end |
#null_object? ⇒ Boolean
31 32 33 |
# File 'lib/spec/mocks/proxy.rb', line 31 def null_object? @options[:null_object] end |
#ok_to_invoke_stub?(stub, expectation) ⇒ Boolean
139 140 141 |
# File 'lib/spec/mocks/proxy.rb', line 139 def ok_to_invoke_stub?(stub, expectation) stub && (!expectation || expectation.called_max_times?) end |
#raise_unexpected_message_args_error(expectation, *args) ⇒ Object
143 144 145 |
# File 'lib/spec/mocks/proxy.rb', line 143 def (expectation, *args) @error_generator. expectation, *args end |
#raise_unexpected_message_error(sym, *args) ⇒ Object
147 148 149 |
# File 'lib/spec/mocks/proxy.rb', line 147 def (sym, *args) @error_generator. sym, *args end |
#received_message?(sym, *args, &block) ⇒ Boolean
92 93 94 |
# File 'lib/spec/mocks/proxy.rb', line 92 def (sym, *args, &block) @messages_received.any? {|array| array == [sym, args, block]} end |
#record_almost_matching_expectation(expectation, sym, *args, &block) ⇒ Object
132 133 134 135 136 137 |
# File 'lib/spec/mocks/proxy.rb', line 132 def record_almost_matching_expectation(expectation, sym, *args, &block) expectation.advise(args, block) unless (null_object? or has_negative_expectation?(sym)) (expectation, *args) end end |
#record_message_received(sym, args, block) ⇒ Object
100 101 102 |
# File 'lib/spec/mocks/proxy.rb', line 100 def (sym, args, block) @messages_received << [sym, args, block] end |
#record_stub(stub, sym, args, &block) ⇒ Object
121 122 123 124 125 126 |
# File 'lib/spec/mocks/proxy.rb', line 121 def record_stub(stub, sym, args, &block) almost_matching_expectation(sym, *args) do |e| e.advise(args, block) end stub.invoke(*args, &block) end |
#remove_stub(message) ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/spec/mocks/proxy.rb', line 68 def remove_stub() = .to_sym if stub_to_remove = @stubs.detect { |s| s.matches_name?() } reset_proxied_method() @stubs.delete(stub_to_remove) else raise MockExpectationError, "The method `#{}` was not stubbed or was already unstubbed" end end |
#reset ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/spec/mocks/proxy.rb', line 84 def reset clear_expectations clear_stubs reset_proxied_methods clear_proxied_methods reset_nil_expectations_warning end |
#verify ⇒ Object
:nodoc:
78 79 80 81 82 |
# File 'lib/spec/mocks/proxy.rb', line 78 def verify #:nodoc: verify_expectations ensure reset end |