diff --git a/lib/shoulda/matchers/doublespeak/method_call.rb b/lib/shoulda/matchers/doublespeak/method_call.rb index 371ac621..4d6cc5cd 100644 --- a/lib/shoulda/matchers/doublespeak/method_call.rb +++ b/lib/shoulda/matchers/doublespeak/method_call.rb @@ -2,6 +2,7 @@ module Shoulda module Matchers module Doublespeak class MethodCall + attr_accessor :return_value attr_reader :method_name, :args, :block, :object, :double def initialize(args) @@ -10,6 +11,13 @@ module Shoulda @block = args[:block] @double = args[:double] @object = args[:object] + @return_value = nil + end + + def with_return_value(return_value) + dup.tap do |call| + call.return_value = return_value + end end def ==(other) diff --git a/lib/shoulda/matchers/doublespeak/proxy_implementation.rb b/lib/shoulda/matchers/doublespeak/proxy_implementation.rb index f7f47b27..6168d3e4 100644 --- a/lib/shoulda/matchers/doublespeak/proxy_implementation.rb +++ b/lib/shoulda/matchers/doublespeak/proxy_implementation.rb @@ -18,8 +18,9 @@ module Shoulda end def call(call) - stub_implementation.call(call) - call.double.call_original_method(call) + return_value = call.double.call_original_method(call) + stub_implementation.call(call.with_return_value(return_value)) + return_value end protected diff --git a/spec/unit/shoulda/matchers/doublespeak/proxy_implementation_spec.rb b/spec/unit/shoulda/matchers/doublespeak/proxy_implementation_spec.rb index e79ce07e..d733780b 100644 --- a/spec/unit/shoulda/matchers/doublespeak/proxy_implementation_spec.rb +++ b/spec/unit/shoulda/matchers/doublespeak/proxy_implementation_spec.rb @@ -13,23 +13,11 @@ module Shoulda::Matchers::Doublespeak end describe '#call' do - it 'delegates to its stub_implementation' do - stub_implementation = build_stub_implementation - call = build_call - implementation = described_class.new(stub_implementation) - - implementation.call(call) - - expect(stub_implementation). - to have_received(:call). - with(call) - end - it 'calls #call_original_method on the double' do stub_implementation = build_stub_implementation double = build_double call = build_call(double: double) - allow(double).to receive(:call_original_method).and_return(call) + allow(double).to receive(:call_original_method) implementation = described_class.new(stub_implementation) implementation.call(call) @@ -38,18 +26,47 @@ module Shoulda::Matchers::Doublespeak to have_received(:call_original_method). with(call) end + + it 'delegates to its stub_implementation' do + stub_implementation = build_stub_implementation + double = build_double + call2 = build_call + call = build_call(double: double, with_return_value: call2) + allow(double).to receive(:call_original_method) + implementation = described_class.new(stub_implementation) + + implementation.call(call) + + expect(stub_implementation). + to have_received(:call). + with(call2) + end + + it 'returns the return value of the original method' do + return_value = :some_return_value + stub_implementation = build_stub_implementation + double = build_double(call_original_method: return_value) + call = build_call(double: double) + implementation = described_class.new(stub_implementation) + + return_value = implementation.call(call) + + expect(return_value).to be return_value + end end def build_stub_implementation double('stub_implementation', returns: nil, call: nil) end - def build_double - double('double', call_original_method: nil) + def build_double(methods = {}) + defaults = { call_original_method: nil } + double('double', defaults.merge(methods)) end - def build_call(double: build_double) - double('call', double: double) + def build_call(methods = {}) + defaults = { double: build_double, with_return_value: nil } + double('call', defaults.merge(methods)) end end end