Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rspec rubocop error `prefer using verifying doubles over normal doubles`

I've a RSpec test

let(:mail_instance) { double(deliver_later: nil) }

before do
  allow(ConfirmationMailer).to receive(:send_email).and_return(mail_instance)
  allow(mail_instance).to receive(:deliver_later)
end

it 'calls mailer, and delivers the mail' do
  call_endpoint
  expect(mail_instance).to have_received(:deliver_later)
end

And it works well but I've got rubocop error - prefer using verifying doubles over normal doubles. According to docs https://www.rubydoc.info/gems/rubocop-rspec/1.7.0/RuboCop/Cop/RSpec/VerifiedDoubles I should use instance_double instead but if I change it my specs will we falls with an error:

ArgumentError: Module or String expected, got {:deliver_later=>nil}

like image 528
mr_muscle Avatar asked Jan 23 '26 07:01

mr_muscle


1 Answers

You need to pass the class you're stubbing to the instance_double:

let(:mail_instance) { instance_double(ActionMailer::MessageDelivery, deliver_later: nil) }
like image 165
mrzasa Avatar answered Jan 24 '26 20:01

mrzasa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!