【问题标题】:Restler, writing test in behat for POST-MethodRestler,为 POST 方法编写测试
【发布时间】:2016-03-24 02:32:09
【问题描述】:

我想在 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 指定“孩子”。

【问题讨论】:

  • Restler RC5 或 RC6 哪个版本?
  • 我使用的是版本 3 RC5

标签: behat restler


【解决方案1】:

解决方案是更改行为特征文件中的顺序

由于RestContext.php bootstrap file 的定义方式,它以这种方式工作。

目前你有什么和测试结果

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

【讨论】:

  • 那你应该将问题标记为已回答,并将我的答案标记为正确的
猜你喜欢
  • 1970-01-01
  • 2018-08-17
  • 2021-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
相关资源
最近更新 更多