【问题标题】:PHPUnit in symfony2 fails on JSON request without error messagesymfony2 中的 PHPUnit 在 JSON 请求上失败,没有错误消息
【发布时间】:2014-12-28 16:57:15
【问题描述】:

我正在尝试调用返回 JSON 的页面。这是控制器发送的响应

    $response = new JsonResponse();
    $response->setData($result);
    $response->send();
    exit;

当我用 phpunit 测试它时,使用以下代码:

    $publicClient = static::createClient();
    $publicClient->request('POST', '/list/', []);
    $this->assertTrue(true);

断言永远不会到达。相反,输出(带有--debug和-v)很简单

bin/phpunit -c app/phpunit.xml.dist --debug -v
PHPUnit 4.3.4 by Sebastian Bergmann.

Configuration read from /var/www/eliphi/public_html/app/phpunit.xml.dist


Starting test '[...]\Tests\Controller\FrontendControllerTest::testListAjax'.
[{"seriesTitle":"public","publications":[]},{"seriesTitle":"member","publications":[]},{"seriesTitle":"abo","publications":[]},{"seriesTitle":"public Magazine","publications":[]},{"seriesTitle":"member Magazine","publications":[]},{"seriesTitle":"abo Magazine","publications":[]}]

仅此而已!当我从浏览器调用页面时,响应标头已正确设置为

Date: Sat, 01 Nov 2014 14:56:31 GMT    
Server: Apache/2.2.22 (Ubuntu)
X-Powered-By: PHP/5.4.34-1+deb.sury.org~precise+1    
Cache-Control: no-cache    
Connection: close    
Content-Type: application/json    

200 OK

如果我对返回 html 的不同 url 发出请求,它就可以正常工作。这可能是什么原因?

【问题讨论】:

    标签: php json symfony phpunit


    【解决方案1】:

    您在控制器中的exit 调用正在终止测试脚本。一般来说,您应该改用return

    从控制器退出将中断测试并绕过任何设置的控制器后事件 - 如果您使用任何第三方捆绑包,可能会产生意想不到的结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-21
      • 2023-03-22
      • 2015-09-27
      • 1970-01-01
      • 1970-01-01
      • 2018-01-28
      相关资源
      最近更新 更多