与编写模拟测试相比,使用 FakeWeb 的缺点

发布于 08-25 15:35 字数 162 浏览 15 评论 0原文

我从来不喜欢写模拟,不久前有人推荐使用 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 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疾风者2024-09-01 15:35:16

您可能会对 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

未蓝澄海的烟2024-09-01 15:35:16

你应该看看 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 客户端的实现细节。

You should take a look at WebMock http://github.com/bblimke/webmock

The downside of mocking http requests is the lack of protection against remote API changes. If remote HTTP service changes, and your code won't be compatible anymore, your tests won't tell you about it. If you mock http client methods on your own, you have the same problems.
It's good to have some integration test suite which verifies that your code can still talk to real http service.

The advantage of library like FakeWeb or WebMock is the fact that you can focus on implementing behaviour instead of worrying about implementation details of specific http client library. Even if you change library from for example Net::HTTP to RestClient, the behaviour should still be preserved so the tests should still be passing. If you mock http client on your own, you have to change tests when you change implementation, even if behaviour didn't change.
Using FakeWeb or Webmock also helps with TDD or BDD (test first). You can specify the http behaviour with specs or tests, before worrying about implementation details of specific http client.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文