我有一个类似的问题,名为featurecontext behat issue,代码400错误请求,返回此值的验证不应为空。答案是一样的。
使用behat,我想使用rest api用多个测试对象填充数据库表。
在执行post命令时,behat api扩展不知何故丢失了请求正文内容,因此改为发送null,我最终创建了空对象,或者收到一个代码500错误,说我不能将null值插入rest对象。
我有以下行为特征:
相册.feature
Feature: Provide a consistent standard JSON API endpoint
In order to build interchangeable front ends
As a JSON API developer
I need to allow Create, Read, Update, and Delete functionality
Background:
Given there are Albums with the following details:
| title | track_count | release_date |
| The Dark Side of the Moon | 12 | 1973-03-24T00:00:00+00:00 |
| Back in Black | 9 | 1980-06-25T23:22:21+00:00 |
| Thriller | 23 | 1982-11-30T11:10:09+00:00 |
And the "Content-Type" request header is "application/json"
...
以及以下功能:
featurecontext.php
...
/**
* @Given there are Albums with the following details:
*/
public function thereAreAlbumsWithTheFollowingDetails(TableNode $albums) {
foreach ($albums->getColumnsHash() as $album) {
$this->apiContext->setRequestBody(json_encode($album));
$this->apiContext->requestPath("/api/album", "POST");
$expectedResult = ["{",'" status": "ok",',"}"];
$this->apiContext->assertResponseBodyIs(new \Behat\Gherkin\Node\PyStringNode($expectedResult,0));
echo 'passed.';
}
}
...
我收到的错误如下:
Expected response body "{
" status": "ok",
}", got "{
"code": 500,
"message": "Unexpected error occured: An exception occurred while executing
'INSERT INTO Album (title, release_date, track_count)
VALUES (?, ?, ?)' with params [null, null, null]:\n\nSQLSTATE [23000, 515]:
[Microsoft][ODBC Driver 11 for SQL Server][SQL Server]
Cannot insert the value NULL into column 'title', table 'LUNCH_QR_TEST.dbo.Album';
column does not allow nulls. INSERT fails.\n
SQLSTATE [01000, 3621]: [Microsoft][ODBC Driver 11 for SQL Server]
[SQL Server]The statement has been terminated."}".
(Imbo\BehatApiExtension\Exception\AssertionFailedException)
用
print_r($this->request->getBody()->getContents());
在里面
ApiContext.php
我已经确定第一张专辑的请求正文内容是:
{"title":"The Dark Side of the Moon","track_count":"12","release_date":"1973-03-24T00:00:00+00:00"}
直到
https://github.com/imbo/behat-api-extension/blob/develop/src/Context/ApiContext.php#L986
.