Last active
February 27, 2024 10:58
-
-
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.
This file contains hidden or 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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There's no support for that: https://github.com/rspec/rspec-mocks/issues/1425#issuecomment-1762783969