【问题标题】:How to test ROR REST api with cucumber如何用黄瓜测试 FOR REST api
【发布时间】:2018-08-04 15:37:08
【问题描述】:

您好,我正在使用 ruby​​-2.5.1 和 Rails 5 开发一个 ROR 项目。我有一个注册请求的 api 如下:

{
  "data": {
    "type": "users",
    "attributes": {
      "email": "test@gmail.com",
      "password": "passwor",
      "password-confirmation" : "password"
    }
  }
}

我想在我的 rails 应用程序中用黄瓜测试这个注册 api。 我从来没有做过黄瓜我试过的是:-

我的功能文件

Feature: Registration Endpoint

  Scenario: User registration
    When I send a POST request to "/api/registrations" with the following:
      | email | test@gmail.com |
      | password   | password |
      | password_confirmation   | password |
    Then the response status should be "200"

我的 steps.rb 文件:

When("I send a POST request to {string} with the following:") do |string, table|
  # table is a Cucumber::MultilineArgument::DataTable
  pending # Write code here that turns the phrase above into concrete actions
end

但我不确定如何实施步骤。请帮我。提前致谢。

【问题讨论】:

  • 你为什么用黄瓜而不是RSpec?在我从事的所有项目中,我都使用 cucumber 来测试前端(业务逻辑),即登录某处,单击按钮显示某些内容,编辑记录。对于 API/请求规范,使用 RSpec 更容易。
  • 是的,我使用 rspec 进行单元测试。但客户想要这个 api 的黄瓜。我们将调用其他 api 进行注册,我们不想使用现有的注册 api。
  • 反正不急的话,明天我可以帮你
  • @MrShemek 你在吗?

标签: ruby-on-rails cucumber


【解决方案1】:

我会这样做:

When(/^I send a POST request to "(.*)" with the following:$/) do |endpoint, table|
  page.driver.post(endpoint, table.rows_hash)
end

Then(/^the response status should be "(.*)"$/) do |response_code|
  expect(page.status_code).to eq(response_code.to_i)
end

在步骤定义中,您始终可以将binding.pry 用于调试并使用save_and_open_page 在浏览器中显示结果。希望对你有帮助!

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2013-05-23
  • 1970-01-01
  • 1970-01-01
  • 2016-10-14
  • 1970-01-01
  • 1970-01-01
  • 2015-04-07
  • 1970-01-01
相关资源
最近更新 更多