문제

I`m trying to write some functional tests for a REST API, created using FOS Rest Bundle. The problem is that when I use the Symfony\Component\BrowserKit, symfony throws me the following error: {"message":"Unable to find template \"AccountBundle:Account:list.html.twig\". .. }

The code that I run is:

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

When I run the request from the browser, it works fine.

Here is the 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);
}

So when in test scenario, instead of returning the JSON tries to load the template.

도움이 되었습니까?

해결책

You should use the $server parameter of the $client-request() method to set the Accept header to application/json. FOSRestBundle has a listener that returns JSON only if the corresponding Accept header is received, otherwise it will search for the template corresponding to the controller.

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top