2015-02-28 22:28:56 -05:00
|
|
|
require 'doublespeak_spec_helper'
|
2014-04-19 20:01:22 -04:00
|
|
|
|
|
|
|
module Shoulda::Matchers::Doublespeak
|
|
|
|
describe StubImplementation do
|
|
|
|
describe '#call' do
|
|
|
|
it 'calls #record_call on the double' do
|
|
|
|
implementation = described_class.new
|
|
|
|
double = build_double
|
2015-02-28 22:47:39 -05:00
|
|
|
call = build_call(double: double)
|
2014-04-19 20:01:22 -04:00
|
|
|
|
2015-02-28 22:47:39 -05:00
|
|
|
allow(double).to receive(:record_call).with(call)
|
2014-04-19 20:01:22 -04:00
|
|
|
|
2015-02-28 22:47:39 -05:00
|
|
|
implementation.call(call)
|
2014-04-19 20:01:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'if no explicit implementation was set' do
|
|
|
|
it 'returns nil' do
|
|
|
|
implementation = described_class.new
|
|
|
|
double = build_double
|
2015-02-28 22:47:39 -05:00
|
|
|
call = build_call(double: double)
|
2014-04-19 20:01:22 -04:00
|
|
|
|
2015-02-28 22:47:39 -05:00
|
|
|
return_value = implementation.call(call)
|
2014-04-19 20:01:22 -04:00
|
|
|
|
|
|
|
expect(return_value).to eq nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'if the implementation was set as a value' do
|
|
|
|
it 'returns the set return value' do
|
|
|
|
implementation = described_class.new
|
|
|
|
implementation.returns(42)
|
|
|
|
double = build_double
|
2015-02-28 22:47:39 -05:00
|
|
|
call = build_call(double: double)
|
2014-04-19 20:01:22 -04:00
|
|
|
|
2015-02-28 22:47:39 -05:00
|
|
|
return_value = implementation.call(call)
|
2014-04-19 20:01:22 -04:00
|
|
|
|
|
|
|
expect(return_value).to eq 42
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'if the implementation was set as a block' do
|
2015-02-28 22:47:39 -05:00
|
|
|
it 'calls the block with the MethodCall object the implementation was called with' do
|
2014-04-19 20:01:22 -04:00
|
|
|
double = build_double
|
|
|
|
expected_object, expected_args, expected_block = :object, :args, :block
|
2015-02-28 22:47:39 -05:00
|
|
|
call = build_call(
|
|
|
|
double: double,
|
|
|
|
object: expected_object,
|
|
|
|
args: expected_args,
|
|
|
|
block: expected_block
|
|
|
|
)
|
2014-04-19 20:01:22 -04:00
|
|
|
actual_object, actual_args, actual_block = []
|
|
|
|
implementation = described_class.new
|
2015-02-28 22:47:39 -05:00
|
|
|
implementation.returns do |actual_call|
|
|
|
|
actual_object = actual_call.object
|
|
|
|
actual_args = actual_call.args
|
|
|
|
actual_block = actual_call.block
|
2014-04-19 20:01:22 -04:00
|
|
|
end
|
|
|
|
|
2015-02-28 22:47:39 -05:00
|
|
|
implementation.call(call)
|
2014-04-19 20:01:22 -04:00
|
|
|
|
|
|
|
expect(actual_object).to eq expected_object
|
|
|
|
expect(actual_args).to eq expected_args
|
|
|
|
expect(actual_block).to eq expected_block
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the return value of the block' do
|
|
|
|
implementation = described_class.new
|
|
|
|
implementation.returns { 42 }
|
|
|
|
double = build_double
|
2015-02-28 22:47:39 -05:00
|
|
|
call = build_call(double: double)
|
2014-04-19 20:01:22 -04:00
|
|
|
|
2015-02-28 22:47:39 -05:00
|
|
|
return_value = implementation.call(call)
|
2014-04-19 20:01:22 -04:00
|
|
|
|
|
|
|
expect(return_value).to eq 42
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'if the implementation was set as both a value and a block' do
|
|
|
|
it 'prefers the block over the value' do
|
|
|
|
implementation = described_class.new
|
|
|
|
implementation.returns(:something_else) { 42 }
|
|
|
|
double = build_double
|
2015-02-28 22:47:39 -05:00
|
|
|
call = build_call(double: double)
|
2014-04-19 20:01:22 -04:00
|
|
|
|
2015-02-28 22:47:39 -05:00
|
|
|
return_value = implementation.call(call)
|
2014-04-19 20:01:22 -04:00
|
|
|
|
|
|
|
expect(return_value).to eq 42
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_double
|
2014-11-07 17:22:20 -05:00
|
|
|
double('double', record_call: nil)
|
2014-04-19 20:01:22 -04:00
|
|
|
end
|
2015-02-28 22:47:39 -05:00
|
|
|
|
|
|
|
def build_call(options = {})
|
|
|
|
defaults = { double: build_double }
|
|
|
|
double('call', defaults.merge(options))
|
|
|
|
end
|
2014-04-19 20:01:22 -04:00
|
|
|
end
|
|
|
|
end
|