在 Ruby 中,使用 Cucumber,我应该模拟对 Web 服务的调用吗?

发布于 2024-09-03 04:04:13 字数 181 浏览 4 评论 0原文

所有,

我正在使用 Cucumber 来对 Ruby 命令行实用程序进行验收测试。该实用程序从网络服务中提取数据。

我知道 Cucumber 用于验收测试并测试整个堆栈,但我仍然需要从网络服务提供一致的回复。

我应该模拟网络服务吗?如果是,怎么办?这里最好的方法是什么?

干杯, 戈登

All,

I'm using Cucumber for acceptance testing a Ruby command line utility. This utility pulls data in from a webservice.

I understand Cucumber is for acceptance testing and tests the whole stack but I still need to provide consistant replies from the webservice.

Should I mock the webservice? If yes, how? What's the best approach here?

Cheers,
Gordon

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

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

发布评论

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

评论(3

嘴硬脾气大 2024-09-10 04:04:14

所以经过一番思考!然后我通过谷歌搜索发现了FakeWeb。正是我所需要的!

查看 Nic 博士的幻灯片< /a> - 尤其是幻灯片 17。

而且这很简单 - 在不到 2 小时的时间里,我成功地设置了它,重写了我的测试,让一切都通过了,并将其全部检查回 git hub!

其他人!

So after a bit of thinking! Then a bit of googling I found FakeWeb. Does exactly what I needed!

Check out Dr Nic's slides - especially slide 17.

And it was easy - in under 2 hours I've managed to set it up, rewrite my tests, get everything passing and check it all back in to git hub!!

HTH others!

倾`听者〃 2024-09-10 04:04:14

我对 Ruby 或 Cucumber 不太熟悉,所以我只能给你一个与你的问题相关的非常笼统的答案,而且它实际上问题多于答案。

  • 网络服务的可靠性如何?如果它们经常宕机,您的测试会时不时地失败,没有什么比追查测试失败的原因却发现是失败更烦人的了。又是这个月的那个时候。
  • 您的 Web 服务能够承受测试的冲击吗?如果您有多个开发人员经常运行这些测试,并且您的 Web 服务位于合作伙伴公司的服务器上,那么他们可能不喜欢您针对他们进行测试。
  • 你相信他们的输出吗?对我来说,不模拟依赖项的最大原因是,如果我不知道我到底将从服务中获得什么类型的数据。如果我使用的服务有详细记录且易于理解,我通常不会嘲笑它们,但如果它们不完全清晰或经常更改,我建议对它们进行测试。
  • 模拟依赖关系有多难?替换依赖关系并不总是那么容易,特别是在事后添加测试代码时。幸运的是,动态语言通常比 Java 容易得多。我仍然会考虑构建一个模拟服务来响应您真正想要的答案需要多少工作。
  • 我从模拟中获得了多少速度优势?集成测试很慢,模拟 Web 服务依赖项会让您的测试运行得更快,快多少?我不知道,但这可能很重要。

这些只是几点,但在选择嘲笑或不嘲笑之前,我至少总是考虑最后三点。

I am not very familiar with Ruby or Cucumber, so I can only give you a very general answer related to your problem, and it actually has more questions than answers.

  • How reliable are the web services? If they are down a lot it will make your tests fail from time to time and there is nothing more annoying than chasing down the reason for a failing test only to realize it was that time of the month again.
  • Can your web services take the pounding from tests? If you have several developers running these tests very often and your web services are on a partner company's server, they might not like the fact that you are testing against them.
  • Do you trust their output? For me the biggest reason not to mock a dependency is if I don't know what sort of data I am exactly going to get from the service. If I am using services that are well documented and easily understandable I usually don't mock them, but if they are not entirely clear or they change often I do recommend testing against them.
  • How hard is it to mock the dependency? Replacing dependencies is not always easy, especially if adding test code afterwards. Luckily in dynamic languages it is usually a lot easier than lets say Java. I would still consider how much work does it take to build a mock service that responds with the answers you are really wanting.
  • How much of a speed benefit I gain from mocking? Integration tests are slow, mocking a web service dependency is gonna make your test run faster, how much faster? I don't know but it probably does matter.

Those are just a few points, but at least the last three I always consider before choosing to mock or not to mock.

明媚殇 2024-09-10 04:04:14

Web 服务的模拟

我会围绕应用程序中对 Web 服务的调用编写一个包装器。
伪代码中的示例

CallWebService (action, options,...) {
    // Code for connectiong to Webservice
}

然后您只需模拟该功能,就像您想要任何其他功能一样,

CallWebService (action, options,...) {
    return true;
}

这样您就可以模拟Web服务,而不必担心它是Web服务或数据库连接或其他什么。你可以让它返回 true 或其他什么。

测试您的代码如何处理来自 Web 服务的响应

为了进一步推进这一想法并使您的测试更加强大,您可以使用某种测试参数或环境参数来控制模拟的 Web 服务中发生的情况方法。然后,您可以成功测试您的代码如何处理来自 Web 服务的不同响应。
再次使用伪代码:

CallWebService (action, options,...) {
    if TEST_WEBSERVICE_PARAMETER == CORRUPT_XML
        return "<xml><</xmy>";
    else if TEST_WEBSERVICE_PARAMETER == TIME_OUT
        return wait(5000);
    else if TEST_WEBSERVICE_PARAMETER == EMPTY_XML
        return "";
    else if TEST_WEBSERVICE_PARAMETER == REALLY_LONG_XML_RESPONSE
        return generate_xml_response(1000000);
}

并进行匹配测试:

should_raise_error_on_empty_xml_response_from_webservice() {
    TEST_WEBSERVICE_PARAMETER = EMPTY_XML;
    CallWebService(action, option, ...);
    assert_error_was_raised(EMPTY_RESPONSE_FROM_WEBSERVICE);
    assert_written_in_log(EMPTY_RESPONSE_LOG_MESSAGE);
}
...

依此类推,您明白了。
请注意,即使我的所有示例都是负面测试用例,这当然也可以用于测试正面测试用例。

请注意,这是我对类似问题的回答的副本:
iPhone 样机网络服务

祝你好运

Mock of the Webservice

I would write a wrapper around the calls to the webservice in the application.
Example in Pseudo Code

CallWebService (action, options,...) {
    // Code for connectiong to Webservice
}

Then you just mock of that function just you would like any other function

CallWebService (action, options,...) {
    return true;
}

This way you can mock of the webservice without bothering about it being a webservice or a database connection or whatever. And you can have it return true or whatever.

Test how your code handles responses from the Webservice

To take this idea one step further and make your tests even more powerful you could use some kind of test parameters or environment parameters to control what happens in the mocked off webservice method. Then you can successfully test how your codes handels different responses from the web services.
Again in pseudo-code:

CallWebService (action, options,...) {
    if TEST_WEBSERVICE_PARAMETER == CORRUPT_XML
        return "<xml><</xmy>";
    else if TEST_WEBSERVICE_PARAMETER == TIME_OUT
        return wait(5000);
    else if TEST_WEBSERVICE_PARAMETER == EMPTY_XML
        return "";
    else if TEST_WEBSERVICE_PARAMETER == REALLY_LONG_XML_RESPONSE
        return generate_xml_response(1000000);
}

And tests to match:

should_raise_error_on_empty_xml_response_from_webservice() {
    TEST_WEBSERVICE_PARAMETER = EMPTY_XML;
    CallWebService(action, option, ...);
    assert_error_was_raised(EMPTY_RESPONSE_FROM_WEBSERVICE);
    assert_written_in_log(EMPTY_RESPONSE_LOG_MESSAGE);
}
...

And so on, you get the point.
Please note that even though all my examples are Negative test cases this could of course be used to test Positive test cases also.

Do note that this is a copy of an answer i made to a similar questions:
Mockup webservice for iPhone

Good luck

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