簡體   English   中英

法拉第的Rspec :: ConnectionFailed

[英]Rspec for Faraday::ConnectionFailed

我在代碼中有以下方法需要為其編寫規范(用於救援塊)。 我嘗試了一些嘗試,但沒有成功,因此我的規格將進入救援階段。 我讀過有關使用存根的信息,但不知道如何在這種情況下使用它

 def start
    some code
  rescue Faraday::ConnectionFailed, Faraday::TimeoutError, Faraday::SSLError => e
    message 'CONNECTION FAILED'
  end

我也讀過關於Webmock寶石,但是有沒有使用任何額外寶石的方法

由於隱私原因,您無法發布您的真實代碼,因此有一般建議。

假設您有以下代碼:

def start
  SomeClass.some_method(some_params)
rescue Faraday::ConnectionFailed, Faraday::TimeoutError, Faraday::SSLError => e
  message 'CONNECTION FAILED'
end

要測試該消息是否引發,您可以執行以下操作:

it 'returns "CONNECTION FAILED" message' do
  allow(SomeClass).to receive(:request_api) { raise(Faraday::ConnectionFailed) }
  # now you call `start` method and check what you want
end

因此,您只想用方法中存在的實際類和方法替換allow(SomeClass).to receive(:some_method)

使用WebMock

使用WebMock可以通過多種方式實現。 最短的是:

it 'returns "CONNECTION FAILED" message' do
  WebMock.disable_net_connect!
  # call `start` and test your rescue block
  WebMock.allow_net_connect!
end

暫無
暫無

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

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