在 Ruby 中,使用 Cucumber,我应该模拟对 Web 服务的调用吗?
所有,
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
所以经过一番思考!然后我通过谷歌搜索发现了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!
我对 Ruby 或 Cucumber 不太熟悉,所以我只能给你一个与你的问题相关的非常笼统的答案,而且它实际上问题多于答案。
这些只是几点,但在选择嘲笑或不嘲笑之前,我至少总是考虑最后三点。
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.
Those are just a few points, but at least the last three I always consider before choosing to mock or not to mock.
Web 服务的模拟
我会围绕应用程序中对 Web 服务的调用编写一个包装器。
伪代码中的示例
然后您只需模拟该功能,就像您想要任何其他功能一样,
这样您就可以模拟Web服务,而不必担心它是Web服务或数据库连接或其他什么。你可以让它返回 true 或其他什么。
测试您的代码如何处理来自 Web 服务的响应
为了进一步推进这一想法并使您的测试更加强大,您可以使用某种测试参数或环境参数来控制模拟的 Web 服务中发生的情况方法。然后,您可以成功测试您的代码如何处理来自 Web 服务的不同响应。
再次使用伪代码:
并进行匹配测试:
依此类推,您明白了。
请注意,即使我的所有示例都是负面测试用例,这当然也可以用于测试正面测试用例。
请注意,这是我对类似问题的回答的副本:
iPhone 样机网络服务
祝你好运
Mock of the Webservice
I would write a wrapper around the calls to the webservice in the application.
Example in Pseudo Code
Then you just mock of that function just you would like any other function
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:
And tests to match:
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