如何用黄瓜测试ROR REST API

时间:2018-08-04 15:37:08

标签: ruby-on-rails cucumber

嗨,我正在使用ruby-2.5.1和Rails 5进行ROR项目。我有一个用于注册请求的api,如下所示:

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

我想在我的Rails应用程序中用黄瓜测试此registartion 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

但是我不确定如何执行步骤。请帮我。预先感谢。

1 个答案:

答案 0 :(得分:0)

我会做这样的事情:

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在浏览器中显示结果。希望对您有所帮助!