如何测试与 Web 服务通信的 Ruby 命令行程序? [英] How can I test a Ruby command-line program that communicates with a web service?

查看:17
本文介绍了如何测试与 Web 服务通信的 Ruby 命令行程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个与 Web 服务通信的 Ruby 命令行程序.我正在使用 Cucumber 和 Aruba 来测试程序.问题是我需要控制从 Web 服务返回的数据;该程序会抓取用户评论流,因此随着新评论的添加,这可能会经常更改.我尝试使用 WebMock 模拟 Web 服务,但这不起作用,因为 Aruba 将命令行程序转为不受 WebMock 影响的单独进程(因此它仍然与 real网络服务).

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆