0
votes

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": "[email protected]",
          "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": "[email protected]",
          "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
Can you update the question with the code responsible for handling this request (from controller)?MrShemek

1 Answers

0
votes

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.