Kev*_*ell 2 tdd bdd ruby-on-rails cucumber capybara
我用黄瓜做很多事情.我非常喜欢它作为BDD环境.
所以我想将它作为外部工具来测试API.我想做的事情如下:
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"
Run Code Online (Sandbox Code Playgroud)
或类似的东西.我主要想将API视为黑盒子,只测试输入和输出.我不打算检查API中的任何内容.
我看过的与Cucumber(例如Capybara)合作的大多数库似乎都是围绕基于Rack的应用程序设计的.我想要类似的东西,但不依赖于Rack.
存在哪些没有机架依赖性的宝石(如果有的话).或者有没有办法使用Capybara来测试远程服务器上的API?
我不会使用Capybara来测试远程API,因为用于测试应用程序的Capybara用于使用HTML UI测试应用程序(正如Aslak在评论中指出的那样).
相反,我会将Cucumber*与HTTParty结合使用,这将是用于发出HTTP请求并整齐地解析它们的工具.这是一个想法:
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
Run Code Online (Sandbox Code Playgroud)
这里的最后一步你会这样使用:
Then I should see the following result:
"""
{ success: true }
"""
Run Code Online (Sandbox Code Playgroud)
*我实际上会亲自使用RSpec,我觉得语法不那么笨拙.