如何使用 Cucumber 测试非 Ruby、非 Rack API

发布于 2024-12-13 00:50:51 字数 465 浏览 0 评论 0原文

我用黄瓜做很多事情。我真的很喜欢它作为 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"

或类似的事情。我主要想将 API 视为黑匣子并仅测试输入和输出。我不打算检查 API 内部的任何内容。

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

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

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

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"

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.

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一个人的旅程 2024-12-20 00:50:51

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

相反,我会将 Cucumber* 与 HTTParty 之类的工具结合使用,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

最后一步你可以像这样使用:

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

* 我个人实际上会使用 RSpec,我发现语法不太笨拙。

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).

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 }
   """

* I would actually use RSpec personally, I find the syntax less clumsy.

记忆之渊 2024-12-20 00:50:51

我已经在 Drupal 应用程序中使用 Cucumber 一段时间了。效果很好。

这帮助我使用 selenium 设置水豚

https://github.com/thuss/standalone-cucumber

如果你想使用mechanize,那就有点麻烦了。我不得不使用 0.3.0-rc3,因为在重定向等之后存在一些问题。提交字段名称包含“[]”字符的表单仍然存在一些问题。我不太记得我团队中的另一个人是如何发现这个错误的。

I've been using cucumber against a Drupal application for some time now. It's working out well.

This helped me set up capybara with selenium

https://github.com/thuss/standalone-cucumber

If you want to use mechanize, it's a bit buggy. I had to use 0.3.0-rc3 as there were some issues following redirects etc. There are still a few issues with submitting forms with field names containing "[]" characters. I can't quite remember as another person on my team discovered that bug.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文