@@ -40,13 +40,16 @@ def as_null_object
40
40
def add_message_expectation ( expected_from , sym , opts = { } , &block )
41
41
__add sym
42
42
warn_if_nil_class sym
43
- if existing_stub = @stubs . detect { |s | s . sym == sym }
44
- expectation = existing_stub . build_child ( expected_from , block_given? ? block : nil , 1 , opts )
43
+ @expectations << build_expectation ( expected_from , sym , opts , &block )
44
+ @expectations . last
45
+ end
46
+
47
+ def build_expectation ( expected_from , sym , opts , &block )
48
+ if stub = find_matching_method_stub ( sym )
49
+ stub . build_child ( expected_from , block_given? ? block : nil , 1 , opts )
45
50
else
46
- expectation = MessageExpectation . new ( @error_generator , @expectation_ordering , expected_from , sym , block_given? ? block : nil , 1 , opts )
51
+ MessageExpectation . new ( @error_generator , @expectation_ordering , expected_from , sym , block_given? ? block : nil , 1 , opts )
47
52
end
48
- @expectations << expectation
49
- @expectations . last
50
53
end
51
54
52
55
def add_negative_message_expectation ( expected_from , sym , &block )
@@ -64,7 +67,6 @@ def add_stub(expected_from, sym, opts={}, &implementation)
64
67
65
68
def remove_stub ( message )
66
69
message = message . to_sym
67
-
68
70
if stub_to_remove = @stubs . detect { |s | s . matches_name? ( message ) }
69
71
reset_proxied_method ( message )
70
72
@stubs . delete ( stub_to_remove )
@@ -92,7 +94,7 @@ def received_message?(sym, *args, &block)
92
94
end
93
95
94
96
def has_negative_expectation? ( sym )
95
- @expectations . detect { |expectation | expectation . negative_expectation_for? ( sym ) }
97
+ @expectations . any? { |expectation | expectation . negative_expectation_for? ( sym ) }
96
98
end
97
99
98
100
def record_message_received ( sym , args , block )
@@ -103,21 +105,39 @@ def message_received(sym, *args, &block)
103
105
expectation = find_matching_expectation ( sym , *args )
104
106
stub = find_matching_method_stub ( sym , *args )
105
107
106
- if ( stub && expectation && expectation . called_max_times? ) || ( stub && !expectation )
107
- if expectation = find_almost_matching_expectation ( sym , *args )
108
- expectation . advise ( args , block ) unless expectation . expected_messages_received?
109
- end
110
- stub . invoke ( *args , &block )
108
+ if ok_to_invoke_stub? ( stub , expectation )
109
+ record_stub ( stub , sym , args , &block )
111
110
elsif expectation
112
- expectation . invoke ( *args , &block )
111
+ invoke_expectation ( expectation , *args , &block )
113
112
elsif expectation = find_almost_matching_expectation ( sym , *args )
114
- expectation . advise ( args , block ) if null_object? unless expectation . expected_messages_received?
115
- raise_unexpected_message_args_error ( expectation , *args ) unless ( has_negative_expectation? ( sym ) or null_object? )
113
+ record_almost_matching_expectation ( expectation , sym , *args , &block )
116
114
else
117
115
@target . __send__ :method_missing , sym , *args , &block
118
116
end
119
117
end
120
118
119
+ def record_stub ( stub , sym , args , &block )
120
+ almost_matching_expectation ( sym , *args ) do |e |
121
+ e . advise ( args , block )
122
+ end
123
+ stub . invoke ( *args , &block )
124
+ end
125
+
126
+ def invoke_expectation ( expectation , *args , &block )
127
+ expectation . invoke ( *args , &block )
128
+ end
129
+
130
+ def record_almost_matching_expectation ( expectation , sym , *args , &block )
131
+ expectation . advise ( args , block )
132
+ unless ( null_object? or has_negative_expectation? ( sym ) )
133
+ raise_unexpected_message_args_error ( expectation , *args )
134
+ end
135
+ end
136
+
137
+ def ok_to_invoke_stub? ( stub , expectation )
138
+ stub && ( !expectation || expectation . called_max_times? )
139
+ end
140
+
121
141
def raise_unexpected_message_args_error ( expectation , *args )
122
142
@error_generator . raise_unexpected_message_args_error expectation , *args
123
143
end
@@ -202,15 +222,11 @@ class << @target; self; end
202
222
end
203
223
204
224
def verify_expectations
205
- @expectations . each do |expectation |
206
- expectation . verify_messages_received
207
- end
225
+ @expectations . map { |e | e . verify_messages_received }
208
226
end
209
227
210
228
def reset_proxied_methods
211
- @proxied_methods . each do |sym |
212
- reset_proxied_method ( sym )
213
- end
229
+ @proxied_methods . map { |sym | reset_proxied_method ( sym ) }
214
230
end
215
231
216
232
def reset_proxied_method ( sym )
@@ -237,10 +253,15 @@ def find_matching_expectation(sym, *args)
237
253
@expectations . find { |expectation | expectation . matches ( sym , args ) }
238
254
end
239
255
256
+ def almost_matching_expectation ( sym , *args , &block )
257
+ if e = find_almost_matching_expectation ( sym , *args )
258
+ yield e
259
+ end
260
+ end
261
+
240
262
def find_almost_matching_expectation ( sym , *args )
241
263
@expectations . find { |expectation | expectation . matches_name_but_not_args ( sym , args ) }
242
264
end
243
-
244
265
end
245
266
end
246
267
end
0 commit comments