首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用cucumber测试ROR REST api

如何使用cucumber测试ROR REST api
EN

Stack Overflow用户
提问于 2018-08-04 23:37:08
回答 1查看 236关注 0票数 0

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

代码语言:javascript
复制
{
  "data": {
    "type": "users",
    "attributes": {
      "email": "test@gmail.com",
      "password": "passwor",
      "password-confirmation" : "password"
    }
  }
}

我想在我的rails应用程序中使用cucumber测试这个registartion api。我从来没有研究过黄瓜,我尝试过的是:

我的功能文件

代码语言:javascript
复制
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文件:

代码语言:javascript
复制
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

但我不确定如何实现这些步骤。请帮帮我。提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2018-08-06 02:01:55

我会这样做:

代码语言:javascript
复制
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在浏览器中显示结果。我希望它能有所帮助!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51687121

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档