测试响应的问题

时间:2018-08-23 11:06:53

标签: symfony behat

我真的很想测试Behat的响应。 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);
}

1 个答案:

答案 0 :(得分:3)

尝试将响应数据作为文本块提供,如下所示:

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

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