如何测试与Web服务通信的Ruby命令行程序? [英] How can I test a Ruby command-line program that communicates with a web service?
问题描述
我正在建立一个与web服务通信的Ruby命令行程序。我使用Cucumber和Aruba测试程序。问题是我需要控制从Web服务返回的数据;该程序抓取用户评论流,因此这可能会随着添加新评论而频繁更改。我尝试使用WebMock模拟Web服务,但这没有工作,因为Aruba将命令行程序关闭到一个单独的进程,不受WebMock影响(所以它仍然与 Web服务)。
I am building a Ruby command-line program that communicates with a web service. I am using Cucumber and Aruba to test the program. The problem is that I need to control the data returned from the web service; the program grabs a stream of user comments, so this can change frequently as new comments are added. I tried to mock the web service using WebMock, but this didn't work, since Aruba spins the command-line program off into a separate process that is unaffected by WebMock (so it still communicated with the real web service).
如何使用Cucumber测试此程序的输出?
How can I test the output of this program using Cucumber?
编辑:Web服务以JSON数据形式返回流。我捕获了用于测试的数据的快照;简而言之,我正在寻找一种替代我的静态数据代替实际调用Web服务的方法。
The web service returns the stream as JSON data. I've captured a snapshot of data to use for testing; in a nutshell, I'm looking for a way to substitute my static data in place of an actual call to the web service.
或者,如果有完全不同的方式
Or, if there's a completely different way to accomplish this goal, I'm all ears.
推荐答案
Aruba提供了一种模式,将允许您使用WebMock或VCR。这是一篇博客文章,解释如何做:
Aruba provides a mode that lets you run things "in process" that will allow you to use WebMock or VCR. Here's a blog post explaining how to do that:
http://georgemcintosh.com/vcr-and-aruba/
或者,您可以考虑编写一个新的二进制文件,首先加载VCR或WebMock,然后加载并执行您的主要二进制文件,并让您的测试运行此二进制文件。
Alternately, you can consider writing a new binary that first loads VCR or WebMock, and then loads and executes your main binary, and have your test run this binary.
这篇关于如何测试与Web服务通信的Ruby命令行程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!