1
0
Fork 0
mirror of https://github.com/thoughtbot/shoulda-matchers.git synced 2022-11-09 12:01:38 -05:00

Doublespeak: Proxies store return value in MethodCall

This commit is contained in:
Elliot Winkler 2015-02-28 22:22:14 -07:00
parent 755b3142a5
commit 0300be8adc
3 changed files with 45 additions and 19 deletions

View file

@ -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)

View file

@ -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

View file

@ -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