测试响应的行为问题

Behat issue with testing response

我真的很难用 Behat 测试响应。 'Then the response should be equal to' 之类的规则未被触发,而它存在于 vendor/behatch/contexts/src/Context/RestContext.php 中。此外,我们可以看到像 Behatch\Context\RestContext::theHeaderShouldBeEqualTo() 这样的其他定义被检测到并且自动工作得很好。那么响应有什么问题?

Feature: Ping feature

  Scenario: Testing ping                                           # features/ping.feature:2
    When I add "Content-Type" header equal to "application/json"        # Behatch\Context\RestContext::iAddHeaderEqualTo()
    And I add "Accept" header equal to "application/json"               # Behatch\Context\RestContext::iAddHeaderEqualTo()
    And I send a "GET" request to "/ping"                               # Behatch\Context\RestContext::iSendARequestTo()
    Then the response status code should be 200                         # Behat\MinkExtension\Context\MinkContext::assertResponseStatus()
    And the response should be in JSON                                  # Behatch\Context\JsonContext::theResponseShouldBeInJson()
    And the header "Content-Type" should be equal to "application/json" # Behatch\Context\RestContext::theHeaderShouldBeEqualTo()
    Then the response should be equal to "pong"

1 scenario (1 undefined)
7 steps (6 passed, 1 undefined)

Behatch\Context\RestContext::theResponseShouldBeEqualTo()

/**
 * Checks, whether the response content is equal to given text
 *
 * @Then the response should be equal to
 * @Then the response should be equal to:
 */
public function theResponseShouldBeEqualTo(PyStringNode $expected)
{
    $expected = str_replace('\"', '"', $expected);
    $actual   = $this->request->getContent();
    $message = "Actual response is '$actual', but expected '$expected'";
    $this->assertEquals($expected, $actual, $message);
}

尝试以文本块的形式提供响应数据,如下所示:

Then the response should be equal to:
    """
    pong
    """

三重引号标记一个文本块,又名 PyStringNode。您将数据作为标量字符串提供。看起来很奇怪,但这就是 Behat 解析此输入的方式。这类似于 Behat 将 markdown 表作为 TableNode 处理的方式。