gpt4 book ai didi

php - Symfony2 FOS RestBundle 测试

转载 作者:行者123 更新时间:2023-11-28 20:20:33 26 4
gpt4 key购买 nike

我正在尝试为使用 FOS Rest Bundle 创建的 REST API 编写一些功能测试。问题是当我使用 Symfony\Component\BrowserKit 时,symfony 抛出以下错误:{"message":"无法找到模板\"AccountBundle:Account:list.html.twig\"..

我运行的代码是:

$client = static::createClient();
$client->request('GET','/account');

当我从浏览器运行请求时,它工作正常。

这是 Controller :

/**
* Get channel by ID
* @Secure(roles="ROLE_USER")
* @RestView()
* @ApiDoc(
* resource=true,
* description="Get channel by id",
* section="Channel",
* output="Channel"
* )
*/
public function getAction(Channel $channel)
{
return array('channel' => $channel);
}

因此,在测试场景中,不是返回 JSON,而是尝试加载模板。

最佳答案

您应该使用 $client-request() 方法的 $server 参数将 Accept header 设置为 application/jsonFOSRestBundle 有一个监听器,仅当收到相应的 Accept header 时才返回 JSON,否则它将搜索与 Controller 对应的模板。

$client->request('GET', '/account', array(), array(), array('HTTP_ACCEPT' => 'application/json'));

关于php - Symfony2 FOS RestBundle 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19404452/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com