rspec如何模拟the fefer_save api调用失败

发布于 01-20 21:46 字数 463 浏览 1 评论 0原文

我有一个具有 before_save 回调的模型。其中,我想调用 API 来计算一些内容并保存它。当 API 不可用并抛出错误时,它应该只保存“不可用”(这完全没问题,这只是我为自己做的一个小项目)。它应该看起来像这样:

class User < ApplicationRecord
  before_save :calc

  private

  def calc
    self.calculated_something = API_call
    rescue StandardError
      self.calculated_something = 'not available'
  end
end

我知道如何测试calculated_something情况(使用VCR),但是如何模拟失败的API调用?顺便说一句,我不想​​使用空的录像带。这是否是保存计算的东西的正确方法?

我希望我的问题很清楚,谢谢。

I have a model that has a before_save callback. In that, I want to call a API to calculate something and save it. When the API is unavailable and throws an error, it then should just save 'not available' (which is totally fine, it's just a small project I do for myself). It should look like this:

class User < ApplicationRecord
  before_save :calc

  private

  def calc
    self.calculated_something = API_call
    rescue StandardError
      self.calculated_something = 'not available'
  end
end

I know how to test the calculated_something case (with VCR), but how do I mock the failing API call? I don't want to use an empty VCR cassette btw. Is this even the correct way to save the calculated_something?

I hope my question is clear, thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文