rspec如何模拟the fefer_save api调用失败
我有一个具有 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论