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