RSpec/AnyInstance: Avoid stubbing using allow_any_instance_of problem

mrzasa

New Member
#1
Help me. I don't understand how to fix it. I tried to many variations...

driver_iq_driver_spec.rb
Mã:
describe '.perform' do
it 'correctly parse response' do
  driver = described_class.new(dot_application, background_check_type, provider_setting).perform
  expect(driver).to be_instance_of(BackgroundCheck)
  expect(driver).to have_attributes(status: 'inprogress', background_check_type_id: 4)
end

context 'when exception when status is Error' do
  before { allow_any_instance_of(described_class).to receive(:driver_iq_api).and_return('https://test/error') }

  it 'returns error message' do
    expect { described_class.new(dot_application, background_check_type, provider_setting).perform }.
      to raise_error(RuntimeError)
  end
 end
end
Error: RSpec/AnyInstance: Avoid stubbing using allow_any_instance_of. before { allow_any_instance_of(described_class).to receive:)driver_iq_api).and_return('https://test/error') }
 

Admin

Administrator
Thành viên BQT
#2
You have a pretty specific instance of your described_class that you can stub:
Mã:
context 'when exception when status is Error' do
  let(:subject) { described_class.new(dot_application, background_check_type, provider_setting) }

  before { allow(subject).to receive(:driver_iq_api).and_return('https://test/error') }

  it 'returns error message' do
    expect { subject.perform }.to raise_error(RuntimeError)
  end
 end
Assuming perform raises the error, not initializing the instance.
 

Từ khóa phổ biến

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top