使用 Cucumber 和 Rspec 通过 BDD 开发 SOAP 客户端

发布于 2024-07-26 04:51:14 字数 238 浏览 1 评论 0原文

作为学习经验,我正在开发一个小型 Rails 应用程序,该应用程序应该查询现有的 SOAP API/Web 服务(使用 Handsoap gem),并将简单地将在那里收集的信息呈现给用户。

我喜欢使用 rspec,并且正在习惯使用 Cucumber 来测试我的应用程序。 让我困惑的部分是如何测试与 API 的交互,即来回传递 XML 请求和响应的“非表示”部分。

非常感谢任何提示和指示!

谢谢!

As a learning experience I'm developing a small Rails application that is supposed to query an existing SOAP API/web service (using the handsoap gem) and will simply present the information gathered there to a user.

I like using rspec and am getting used to cucumber for testing my applications. The part that has me stumped is how to test the interaction with the API, i.e. the 'non-presentation' part of passing back and forth XML requests and responses.

Any tips and pointers are very much appreciated!

Thanks!

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

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

发布评论

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

评论(3

妞丶爷亲个 2024-08-02 04:51:14

看看 fakeweb gem。

它有助于破坏网络互操作性,
即您可以创建测试响应:

  FakeWeb.register_uri(:get, "http://example.com/test1", :body => "Hello World!")

并禁用发送 http 请求的功能:

  FakeWeb.allow_net_connect = false
  Net::HTTP.get(URI.parse("http://example.com/"))
  => raises FakeWeb::NetConnectNotAllowedError

Take a look at fakeweb gem.

It helps to stub network interoperability,
i.e. you can create test responses:

  FakeWeb.register_uri(:get, "http://example.com/test1", :body => "Hello World!")

and disable ability to send http requests:

  FakeWeb.allow_net_connect = false
  Net::HTTP.get(URI.parse("http://example.com/"))
  => raises FakeWeb::NetConnectNotAllowedError
半城柳色半声笛 2024-08-02 04:51:14

我假设您正在谈论功能/集成样式测试而不是单元测试,在这种情况下,我可能会考虑模拟 Web 服务的接口并在较低级别上对代码进行单元测试。

但回到黄瓜。 正如 Aslak Hellesøy 在 Stack Overflow q 810410 中指出的那样,没有什么可以阻止您做这个。 当您编写了实际要测试的行为的场景后,您只需在步骤定义中实例化正确的对象并调用适当的方法即可。 如果您需要的话,也可能将其与返回值或内容的一些断言结合起来。 请参阅 Rspec 期望了解如何加载 rspec 的 should()用黄瓜。

I'm assuming you're talking about functional/integration style tests and not unit tests in which case I would probably consider mocking the interface for the web service and unit testing the code on a lower level.

But to return to cucumber. As Aslak Hellesøy points out in Stack overflow q 810410 there is nothing that should prevent you from doing this. When you have written the scenarios for what behaviour you are actually going to test you just have to instantiate the right objects in the step definitions and call the appropriate methods. Possibly also combining it with a few asserts on return values or content if that is what you need. See Rspec expectations for how to load rspec's should() with cucumber.

如歌彻婉言 2024-08-02 04:51:14

使用最新版本的 Handsoap,您可以使用模拟 http 驱动程序,在 http 级别进行模拟。 请参阅:Http::HttpMock 以及 tests/ 中的示例account_test.rb

With the newest version of Handsoap, you can use a mock http-driver, to mock out at the http-level. See: Http::HttpMock and the example in tests/account_test.rb

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