Sebastian Stroe
Sebastian Stroe

Reputation: 11

Restler, writing test in behat for POST-Method

I want to write a TestScenario in Behat for a POST Method that takes an Object as input.

Feature: Testing The ChildController Endpoint

  Scenario: Trying to create an Child by POSTing vars with an existing Id
    Given that I want to make a new "Child"
    And the request is sent as JSON
    And its "screening_id" is "999888777666"
    And his "first_name" is "John"
    And his "last_name" is "Doe"
    And his "birth_date" is "1979-01-01"
    And his "gender" is "m"
    When I request "/v1/child"
    Then the response status code should be 409
    And the response should be JSON
    And the response has a "error" property

My Method is like:

/**
 * Child Endpoint v1
 *
 * @url POST child/
 * @status 201
 *
 * @param Child $child JSON data 
 * @return application/json
 * @throws RestException
 */
function insertChild(Child $child){......}

My JSON Object that I pass looks like this:

{
"child": {
    "screening_id": "",
    "first_name": "",
    "last_name": "",
    "birth_date": "",
    "gender": ""
}

}

Now... when I make a request with Postman with only:

{
    "screening_id": "",
    "first_name": "",
    "last_name": "",
    "birth_date": "",
    "gender": ""
}

it works fine. But when I'm running the Behat Test, it says that I specified an invalid value for 'child' and I get:

"error": {
      "code": 400,
      "message": "Bad Request: Invalid value specified for 'child'. Expecting an item of type `v1\\models\\Child`"

}

It works from everywhere without specifying the 'child' only from BeHat not.

Upvotes: 1

Views: 575

Answers (1)

Arul Kumaran
Arul Kumaran

Reputation: 993

Solution is to change the order of things in your behat feature file

It works this way because of the way RestContext.php bootstrap file is defined.

Currently what you have and the test result

Feature: Testing The ChildController Endpoint

  Scenario: Trying to create an Child by POSTing vars with an existing Id # features/test.feature:3
    Given that I want to make a new "Child"                               # RestContext::thatIWantToMakeANew()
    And the request is sent as JSON                                       # RestContext::theRequestIsSentAsJson()
    And its "screening_id" is "999888777666"                              # RestContext::thatItsStringPropertyIs()
    And his "first_name" is "John"                                        # RestContext::thatItsStringPropertyIs()
    And his "last_name" is "Doe"                                          # RestContext::thatItsStringPropertyIs()
    And his "birth_date" is "1979-01-01"                                  # RestContext::thatItsStringPropertyIs()
    And his "gender" is "m"                                               # RestContext::thatItsStringPropertyIs()
    When I request "/v1/child"                                            # RestContext::iRequest()

|  POST /behat_test/v1/child HTTP/1.1
|  Host: localhost
|  User-Agent: Guzzle/3.1.2 curl/7.43.0 PHP/5.6.15
|  Content-Type: application/json; charset=utf-8
|  Content-Length: 2
|  
|  []
|  HTTP/1.1 400 Bad Request
|  Date: Sun, 20 Dec 2015 04:21:56 GMT
|  Server: Apache/2.4.16 (Unix) PHP/5.5.30
|  X-Powered-By: Luracast Restler v3.0.0rc5
|  Vary: Accept
|  Cache-Control: no-cache, must-revalidate
|  Expires: 0
|  Content-Language: en
|  Content-Length: 468
|  Connection: close
|  Content-Type: application/json; charset=utf-8
|  
|  {
|      "error": {
|          "code": 400,
|          "message": "Bad Request: Invalid value specified for `child`. Expecting an item of type `Child`"
|      },

If you look closely the request is sending only an empty array

|  POST /behat_test/v1/child HTTP/1.1
|  Host: localhost
|  User-Agent: Guzzle/3.1.2 curl/7.43.0 PHP/5.6.15
|  Content-Type: application/json; charset=utf-8
|  Content-Length: 2
|  
|  []

Fix is to move And the request is sent as JSON below all property definitions. See below

Feature: Testing The ChildController Endpoint

  Scenario: Trying to create an Child by POSTing vars with an existing Id # features/test.feature:3
    Given that I want to make a new "Child"                               # RestContext::thatIWantToMakeANew()
    And its "screening_id" is "999888777666"                              # RestContext::thatItsStringPropertyIs()
    And his "first_name" is "John"                                        # RestContext::thatItsStringPropertyIs()
    And his "last_name" is "Doe"                                          # RestContext::thatItsStringPropertyIs()
    And his "birth_date" is "1979-01-01"                                  # RestContext::thatItsStringPropertyIs()
    And his "gender" is "m"                                               # RestContext::thatItsStringPropertyIs()
    And the request is sent as JSON                                       # RestContext::theRequestIsSentAsJson()
    When I request "/v1/child"                                            # RestContext::iRequest()

|  POST /behat_test/v1/child HTTP/1.1
|  Host: localhost
|  User-Agent: Guzzle/3.1.2 curl/7.43.0 PHP/5.6.15
|  Content-Type: application/json; charset=utf-8
|  Content-Length: 108
|  
|  {"screening_id":"999888777666","first_name":"John","last_name":"Doe","birth_date":"1979-01-01","gender":"m"}

Upvotes: 0

Related Questions