与编写模拟测试相比,使用 FakeWeb 的缺点
我从来不喜欢写模拟,不久前有人推荐使用 FakeWeb。我立刻就完全爱上了 FakeWeb。然而,我想知道使用 FakeWeb 是否有缺点。看起来模拟仍然更常见,所以我想知道我错过了什么,而使用 FakeWeb 是错误的。是否存在 Fakeweb 无法覆盖的某种错误,或者是否与 TDD 或 BDD 流程有关?
I never liked writing mocks and a while ago someone here recommended to use FakeWeb. I immediately fell completely in love with FakeWeb. However, I have to wonder if there is a downside to using FakeWeb. It seems like mocks are still much more common, so I wonder what I am missing that's wrong with using FakeWeb instead. Is there a certain kind of error you can't cover with Fakeweb or is it something about the TDD or BDD process?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(2)
你应该看看 WebMock http://github.com/bblimke/webmock
模拟 http 的缺点requests 的问题是缺乏针对远程 API 更改的保护。如果远程 HTTP 服务发生变化,并且您的代码将不再兼容,您的测试将不会告诉您这一点。如果您自己模拟 http 客户端方法,也会遇到同样的问题。
最好有一些集成测试套件来验证您的代码是否仍然可以与真正的 http 服务通信。
FakeWeb 或 WebMock 等库的优点是您可以专注于实现行为,而不用担心特定 http 客户端库的实现细节。即使您将库从 Net::HTTP 更改为 RestClient,该行为仍应保留,因此测试仍应通过。如果您自己模拟 http 客户端,则即使行为没有改变,您也必须在更改实现时更改测试。
使用 FakeWeb 或 Webmock 还有助于 TDD 或 BDD(首先测试)。您可以通过规范或测试来指定 http 行为,然后再担心特定 http 客户端的实现细节。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
您可能会对 VCR 感到满意,它是 HTTP 模拟库的包装器。它会缓存原始 HTTP 请求,以便您的测试快速且离线工作,但也可以更改为再次执行原始请求,以查看测试是否针对远程 API 有效。
https://github.com/myronmarston/vcr
You will probably get happy with VCR, which is a wrapper around HTTP mocking libraries. It caches the original HTTP requests, so that your tests are fast and work offline, but can also be changed to execute the original request again, to see if the test works against the remote API.
https://github.com/myronmarston/vcr