Last active
August 29, 2015 13:56
-
-
Save yujinakayama/8847524 to your computer and use it in GitHub Desktop.
The problematic behavior of `and_return` without arguments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe '`and_return` without arguments' do | |
let(:obj) { double('obj') } | |
context 'with `stub`' do | |
context 'and `{ }` block' do | |
it 'works properly' do | |
obj.stub(:foo).and_return { | |
'a return value' | |
} | |
expect(obj.foo).to eq('a return value') | |
end | |
end | |
context 'and `do end` block' do | |
it 'works properly' do | |
obj.stub(:foo).and_return do | |
'a return value' | |
end | |
expect(obj.foo).to eq('a return value') | |
end | |
end | |
end | |
context 'with `allow(...).to receive(...)`' do | |
context 'and `{ }` block' do | |
it 'works properly' do | |
# The block is taken by #and_return. | |
allow(obj).to receive(:foo).and_return { | |
'a return value' | |
} | |
expect(obj.foo).to eq('a return value') | |
end | |
end | |
context 'and `do end` block' do | |
it 'returns nil unexpectedly' do | |
# The block is taken by #to. | |
allow(obj).to receive(:foo).and_return do | |
'a return value' | |
end | |
# Describing the unexpected behavior, | |
# this should not pass, but it passes. | |
expect(obj.foo).to be_nil | |
end | |
end | |
end | |
context 'without block' do | |
it 'returns nil' do | |
# Shouldn't this raise error? | |
allow(obj).to receive(:foo).and_return | |
expect(obj.foo).to be_nil | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment