背景场景Symfony FOSTRestBundle Rest Behat / guzzle问题,代码500,请求正文在发送时变为空

时间:2018-06-15 14:09:43

标签: php symfony guzzle behat gherkin

  

我有一个类似的问题,名为FeatureContext Behat问题,Code 400 Bad request,验证返回“此值不应为空白。”这是用相同的答案解决的。

使用Behat我想使用Rest API用多个测试对象填充数据库表。

在执行POST命令时,behat-api-extension以某种方式丢失了请求正文内容,因此反而发送了null而我最终创建了空对象或收到代码500错误,说我无法插入空值REST对象。

我有以下Behat功能:

album.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

1 个答案:

答案 0 :(得分:0)

必须在发送相册之前设置标题:

相册功能

  ...
  Background:
    Given the "Content-Type" request header is "application/json"
    And there are Albums with the following details:
  ...