Skip to content

Instantly share code, notes, and snippets.

@yujinakayama
Last active August 29, 2015 13:56
Show Gist options
  • Save yujinakayama/8847524 to your computer and use it in GitHub Desktop.
Save yujinakayama/8847524 to your computer and use it in GitHub Desktop.
The problematic behavior of `and_return` without arguments
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