如何使用Cucumber测试非Ruby,非Rack API [英] How to use Cucumber to test non-Ruby, non-Rack API's
问题描述
我用黄瓜做很多事情。我真的很喜欢它作为一个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。
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.
存在没有机架依赖性的宝石。或者有办法使用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).
黄瓜*与类似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
将使用像这样:
Then I should see the following result:
"""
{ success: true }
"""
*我实际上会使用RSpec我发现语法更笨拙。
* I would actually use RSpec personally, I find the syntax less clumsy.
这篇关于如何使用Cucumber测试非Ruby,非Rack API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!