簡體   English   中英

Restler,用behat為POST方法編寫測試

[英]Restler, writing test in behat for POST-Method

我想在Behat中為將對象作為輸入的POST方法編寫TestScenario。

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

我的方法就像:

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

我通過的JSON對象如下所示:

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

}

現在...當我僅向Postman提出請求時:

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

它工作正常。 但是當我運行Behat測試時,它說我為'child'指定了無效的值,我得到:

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

}

它可以在任何地方工作,而無需從BeHat指定“孩子”。

解決方案是更改behat功能文件中的事物順序

由於定義RestContext.php引導程序文件的方式,因此它可以工作。

當前您所擁有的和測試結果

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`"
|      },

如果您仔細查看,該請求僅發送一個空數組

|  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
|  
|  []

解決的方法是移動And the request is sent as JSON到所有屬性定義下。 見下文

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"}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM