expected 422 got 200 Cucumber with rails

好久不见. 提交于 2019-12-11 17:51:52

问题


Hi i am working on a ROR project with ruby-2.5.1 and rails 5. I am using cucumber in my rails app to test api i am new with cucumber. when i am trying to define feature for invalid data i am getting the error expected 422 got 200.

my feature file:

Feature: Registration Endpoint

  Scenario: User registration
    Given an application with application_id "1"
    When the client make a valid POST /registartions request with application_id: "1"
    Then response should have status 200

  Scenario: using blank application id
    When the client make a POST /registartions request with blank application-id
    Then response should have status 422 and JSON:
    """
      { "error": "application_id does not exists" }
    """

my steps file:

Given("an application with application_id {string}") do |string|
  string
end

When("the client make a valid POST \/registartions request with application_id: {string}") do |string|
  params = {
      "data":{
        "type":"users",
        "attributes":{
          "email": "s2@gmail.com",
          "password":"password",
          "password-confirmation":"password"
        }
      }
    }
    header 'application-id', "#{string}"
  post '/api/registrations', params
end

Then("response should have status {int}") do |int|
  expect(last_response.status).to be(int)
end

When("the client make a POST \/registartions request with blank application-id") do
  params = {
      "data":{
        "type":"users",
        "attributes":{
          "email": "s2@gmail.com",
          "password":"password",
          "password-confirmation":"password"
        }
      }
    }
    header 'application-id', ''
  post '/api/registrations', params
end

Then("response should have status {int} and JSON:") do |int, string|
  expect(last_response.status).to be(int)
end

Please help me to fix this issue i am writting this cucumber first time so i don't have the idea how to test with invalid data. Please help me. Thanks in advance.


回答1:


It looks like you may have found a bug in your application.

If it is meant to respond with a 422 "Unprocessable Entity" response when you don't include the application id, and it's responding with a 200 (OK), then that would seem like the system under test has an issue.



来源:https://stackoverflow.com/questions/51697138/expected-422-got-200-cucumber-with-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!