我是Ruby的新手,我想写一个规范反对logger expection使用allow。请帮忙,谢谢。Ruby:如何测试异常
电流规格
it 'raises exception fetching auditables' do
allow(NightLiaison::HttpConnection.open).to receive(:get).and_raise(StandardError)
expect { described_class.fetch_auditables }.to raise_error('Fetching auditables raised an exception')
end
末
方法:
def self.fetch_auditables
HttpConnection.open.get AppConfig.api.nightliaison.fetch_auditables
rescue => e
LOGGER.error('Fetching auditables raised an exception', exception: e)
Faraday::Response.new
end
错误消息:
got #<WebMock::NetConnectNotAllowedError: Real HTTP connections are disabled. Unregistered request: GET h... => 200, :body => "", :headers => {})
因为您已启用WebMock在测试中*所有*尝试进行网络连接会导致'WebMock :: NetConnectNotAllowedError'从你贴什么,我会假设'NightLiaison :: HttpConnection.open'被适当地模拟,但也许不是? – GSP
你是对的Nightliason :: HttpConnection.open不是在模拟规范。我需要创建一个webmock存根请求吗? – Skipoura