Skip to content

Instantly share code, notes, and snippets.

@esaborit4code
Last active February 27, 2024 10:58
Show Gist options
  • Save esaborit4code/a64d7edba11ad0e36c197f929928579e to your computer and use it in GitHub Desktop.
Save esaborit4code/a64d7edba11ad0e36c197f929928579e to your computer and use it in GitHub Desktop.
Demonstration of issue in rspec-mocks. All these tests should fail, but the misbehavior makes "with double with `have_received`" pass.
require 'spec_helper'
class Dummy
def go(a:)
end
end
RSpec.describe 'test' do
# All these tests should fail, but the misbehavior makes "with double with `have_received`" pass.
subject { Dummy.new.go(args) }
let(:args) { { a: 1 } }
before do
allow(Dummy).to receive(:new).and_return(repo)
end
shared_examples 'test' do
context 'with `receive`' do
it 'test' do
expect(repo).to receive(:go).with(a: 1)
subject
end
end
context 'with `allow` without args and `receive`' do
before { allow(repo).to receive(:go) }
it 'test' do
expect(repo).to receive(:go).with(a: 1)
subject
end
end
context 'with `allow` with args and `receive`' do
before { allow(repo).to receive(:go).with(a: 1) }
it 'test' do
expect(repo).to receive(:go).with(a: 1)
subject
end
end
context 'with `have_received`' do
before { allow(repo).to receive(:go).with(a: 1) }
it 'test' do
subject
expect(repo).to have_received(:go).with(a: 1)
end
end
end
context 'with a double' do
let(:repo) { instance_double(Dummy, go: nil) }
include_examples 'test'
end
context 'with a real object' do
let(:repo) { Dummy.new }
include_examples 'test'
end
end
@esaborit4code
Copy link
Author

There's no support for that: rspec/rspec-mocks#1425 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment