如何使用 Cucumber 测试非 Ruby、非 Rack API [英] How to use Cucumber to test non-Ruby, non-Rack API's

查看:30
本文介绍了如何使用 Cucumber 测试非 Ruby、非 Rack API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用黄瓜做很多事情.我真的很喜欢它作为 BDD 环境.

I use cucumber for lots of things. I really like it as a BDD environment.

所以我想将它用作测试 API 的外部工具.我想做这样的事情:

So I'd like to use it as an external tool to test an API. I'd like to do things like:

  Scenario: Hit api /info path and get info back
    When I visit the API path '/info'
    Then I should see the following text "Here's info on the API"

或类似的东西.我主要想把 API 当作一个黑匣子,只测试输入和输出.我不打算检查 API 中的任何内容.

or something similar. I mainly want to treat the API as a black box and test only inputs and outputs. I don't plan on inspecting anything inside the API.

我看过的大多数与 Cucumber 一起工作的库(例如 Capybara)似乎都是围绕基于 Rack 的应用程序设计的.我想要类似的东西,但不依赖于 Rack.

Most of the libraries I've looked at that work with Cucumber (for example Capybara) seem to be designed around Rack-based applications. I'd like something similar to that but with no dependency on Rack.

存在哪些不依赖机架的 gem(如果有).或者有没有办法使用 Capybara 来测试远程服务器上的 API?

What gems, if any, exist that have no rack dependencies. Or is there a way to use Capybara to test an API that's on a remote server?

推荐答案

我不会使用 Capybara 来测试远程 API,因为 Capybara 是为测试应用程序而设计的,用于测试具有 HTML UI 的应用程序(正如 Aslak 在评论).

I wouldn't use Capybara to test a remote API because Capybara is made for testing applications is used for testing applications with a HTML UI (as Aslak points out in the comments).

相反,我会将 Cucumber* 与 HTTParty 之类的工具结合使用,后者是用于发出 HTTP 请求并巧妙地解析它们的工具.这是一个想法:

Instead, I would use Cucumber* in conjunction with something like HTTParty which would be the tool used to make the HTTP requests and parse them neatly. Here's an idea:

When /^I visit the API path '(.*?)'/ do |path|
  @result = HTTParty.get("http://theapi.com/#{path}")
end

Then /^I should see the following result:$/ do |result|
  @result.should == result
end

这里的最后一步是这样使用的:

The final step here you would use like this:

Then I should see the following result:
   """
     { success: true }
   """

* 我个人会实际使用 RSpec,我觉得语法不那么笨拙.

这篇关于如何使用 Cucumber 测试非 Ruby、非 Rack API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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