我是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