【问题标题】:RSpec/AnyInstance: Avoid stubbing using allow_any_instance_of problemRSpec/AnyInstance:避免使用allow_any_instance_of问题存根
【发布时间】:2018-11-15 13:41:48
【问题描述】:

帮帮我。我不明白如何解决它。我尝试了很多变化...

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( describe_class).to receive(:driver_iq_api).and_return('https://test/error') }

之前

【问题讨论】:

    标签: ruby-on-rails ruby rspec


    【解决方案1】:

    您有一个非常具体的 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 引发错误,而不是初始化实例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 2011-03-20
      • 1970-01-01
      • 1970-01-01
      • 2011-09-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多