簡體   English   中英

RSpec/AnyInstance:使用 allow_any_instance_of 問題避免存根

[英]RSpec/AnyInstance: Avoid stubbing using allow_any_instance_of problem

幫我。 我不明白如何解決它。 我嘗試了很多變化......

driver_iq_driver_spec.rb

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

錯誤:RSpec/AnyInstance:避免使用allow_any_instance_of 進行存根。 之前 { allow_any_instance_of(描述的類)。接收(:driver_iq_api)。and_return(' https://test/error ')}

你有你的一個非常具體的實例described_class您可以存根:

context 'when exception when status is Error' do
  let(:subject) do
    described_class.new(dot_application, background_check_type, provider_setting)
  end
  
  before do
    allow(subject).to receive(:driver_iq_api).and_return('https://test/error')
  end

  it 'returns error message' do
    expect { subject.perform }.to raise_error(RuntimeError)
  end
 end

假設perform引發錯誤,而不是初始化實例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM