代码之家  ›  专栏  ›  技术社区  ›  rmsluimers

后台场景symfony fostrestbundle rest behat/guzzle问题,代码500,发送时请求正文变为空

  •  0
  • rmsluimers  · 技术社区  · 6 年前

    我有一个类似的问题,名为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 .

    1 回复  |  直到 6 年前
        1
  •  0
  •   rmsluimers    6 年前

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

    相册.feature

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