ruby-on-rails - RSpec/AnyInstance:避免使用allow_any_instance_of问题存根
问题描述
帮我。我不明白如何解决它。我尝试了很多变化......
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 ') }
解决方案
您有一个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
引发错误,而不是初始化实例。
推荐阅读
- android - 视图在全屏协调器布局中被推出屏幕,即隐藏导航按钮
- haskell-stack - 堆栈上传只是挂起
- vue.js - Vue js路由单个URL
- sum - Teradata SQL:如何使用 SUM 函数根据情况 A 或 B 将总计分为两组?
- php - 仅在 PHP 中获取两个日期之间的时间?
- c++14 - 被调用的函数实例必须依赖于参数
- uitableview - UITableView 的单元格中只显示一种类型的信息
- javascript - android:React Native 从另一个应用程序打开一个应用程序?
- c# - C# 如果不为 NULL,则仅分配日期时间值
- r - 如何根据在 RShiny 中的 DateInput 中输入的值填充值