Permalink
Browse files

Revert "cleanup mock proxy" [#1009]

This commit unintentionally changed the behaviour of stubs' return values.
http://rspec.lighthouseapp.com/projects/5645-rspec/tickets/1009

This reverts commit 386e334.
  • Loading branch information...
1 parent 15a2628 commit ce44809469642ac3a8c3f975cb247fc983f8eb2e @tomstuart tomstuart committed with Oct 5, 2010
Showing with 22 additions and 43 deletions.
  1. +22 −43 lib/spec/mocks/proxy.rb
View
@@ -40,16 +40,13 @@ def as_null_object
def add_message_expectation(expected_from, sym, opts={}, &block)
__add sym
warn_if_nil_class sym
- @expectations << build_expectation(expected_from, sym, opts, &block)
- @expectations.last
- end
-
- 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)
+ if existing_stub = @stubs.detect {|s| s.sym == sym }
+ expectation = existing_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)
+ expectation = MessageExpectation.new(@error_generator, @expectation_ordering, expected_from, sym, block_given? ? block : nil, 1, opts)
end
+ @expectations << expectation
+ @expectations.last
end
def add_negative_message_expectation(expected_from, sym, &block)
@@ -67,6 +64,7 @@ def add_stub(expected_from, sym, opts={}, &implementation)
def remove_stub(message)
message = message.to_sym
+
if stub_to_remove = @stubs.detect { |s| s.matches_name?(message) }
reset_proxied_method(message)
@stubs.delete(stub_to_remove)
@@ -94,7 +92,7 @@ def received_message?(sym, *args, &block)
end
def has_negative_expectation?(sym)
- @expectations.any? {|expectation| expectation.negative_expectation_for?(sym)}
+ @expectations.detect {|expectation| expectation.negative_expectation_for?(sym)}
end
def record_message_received(sym, args, block)
@@ -105,41 +103,23 @@ def message_received(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)
+ if (stub && expectation && expectation.called_max_times?) || (stub && !expectation)
+ if expectation = find_almost_matching_expectation(sym, *args)
+ expectation.advise(args, block) unless expectation.expected_messages_received?
+ end
+ stub.invoke(*args, &block)
elsif expectation
- invoke_expectation(expectation, *args, &block)
+ expectation.invoke(*args, &block)
elsif expectation = find_almost_matching_expectation(sym, *args)
- record_almost_matching_expectation(expectation, sym, *args, &block)
+ expectation.advise(args, block) if null_object? unless expectation.expected_messages_received?
+ raise_unexpected_message_args_error(expectation, *args) unless (has_negative_expectation?(sym) or null_object?)
elsif @target.is_a?(Class)
@target.superclass.send(sym, *args, &block)
else
@target.__send__ :method_missing, sym, *args, &block
end
end
- def record_stub(stub, sym, args, &block)
- almost_matching_expectation(sym, *args) do |e|
- e.advise(args, block)
- end
- stub.invoke(*args, &block)
- end
-
- def invoke_expectation(expectation, *args, &block)
- expectation.invoke(*args, &block)
- end
-
- def record_almost_matching_expectation(expectation, sym, *args, &block)
- expectation.advise(args, block)
- unless (null_object? or has_negative_expectation?(sym))
- raise_unexpected_message_args_error(expectation, *args)
- end
- end
-
- def ok_to_invoke_stub?(stub, expectation)
- stub && (!expectation || expectation.called_max_times?)
- end
-
def raise_unexpected_message_args_error(expectation, *args)
@error_generator.raise_unexpected_message_args_error expectation, *args
end
@@ -223,11 +203,15 @@ class << @target; self; end
end
def verify_expectations
- @expectations.map {|e| e.verify_messages_received}
+ @expectations.each do |expectation|
+ expectation.verify_messages_received
+ end
end
def reset_proxied_methods
- @proxied_methods.map {|sym| reset_proxied_method(sym)}
+ @proxied_methods.each do |sym|
+ reset_proxied_method(sym)
+ end
end
def reset_proxied_method(sym)
@@ -254,15 +238,10 @@ def find_matching_expectation(sym, *args)
@expectations.find {|expectation| expectation.matches(sym, args)}
end
- def almost_matching_expectation(sym, *args, &block)
- if e = find_almost_matching_expectation(sym, *args)
- yield e
- end
- end
-
def find_almost_matching_expectation(sym, *args)
@expectations.find {|expectation| expectation.matches_name_but_not_args(sym, args)}
end
+
end
end
end

0 comments on commit ce44809

Please sign in to comment.