正在测试经过身份验证的symfony API-找不到模板

正在测试经过身份验证的symfony API-找不到模板,symfony,Symfony,我尝试为我的symfonyapi(symfony版本:3.4)编写测试 我的symfony项目是用来做API的。我使用fosRest包 尝试调用入口点时出现以下错误: 找不到模板“”(查看:C:\wamp64\www\myproject\src\app/Resources/views,C:\wamp64\www\myproject\src\vendor\symfony\symfony\src\symfony\Bridge\Twig/Resources/views/Form) 当我用xdebug运

我尝试为我的symfonyapi(symfony版本:3.4)编写测试

我的symfony项目是用来做API的。我使用fosRest包

尝试调用入口点时出现以下错误:

找不到模板“”(查看:C:\wamp64\www\myproject\src\app/Resources/views,C:\wamp64\www\myproject\src\vendor\symfony\symfony\src\symfony\Bridge\Twig/Resources/views/Form)

当我用xdebug运行测试时,它通过了我的控制器,得到了我需要的结果

HttpKernel
中,我在这一行看到了我的实体:

$response = \call_user_func_array($controller, $arguments);
但是在这一行之后,它进入视图块并返回这个错误

这是配置问题吗

我的控制器中的入口点:

/**
 * @param $id
 * @Get("/metiers/{id}")
 * @return \AppBundle\Entity\Metier
 */
public function getMetierAction($id)
{
    return $this->getDoctrine()->getManager()->getRepository('AppBundle:Metier')->findOneById($id);
}
这是我的测试:

    $client = static::createClient([], [
        'PHP_AUTH_USER' => 'myuser',
        'PHP_AUTH_PW'   => 'pa$$word',
    ]);

    $client->request('GET', 'http://myproject.localhost/metiers/1');
    $response = $client->getResponse();
    $content = json_decode($response->getContent(), true);
我的config_test.yml文件:

imports:
- { resource: parameters_test.yml }
- { resource: config.yml }

framework:
    test: true

security:
    firewalls:
        # replace 'main' by the name of your own firewall
        main:
            http_basic: ~
fos_rest:
    format_listener:
        rules:
            - { path: '^/api', fallback_format: json }
            - { path: '^/', fallback_format: json }
xdebug中的调用堆栈:

InvalidArgumentException:找不到模板“”(查看:C:\wamp64\www\myproject\src\app/Resources/views,C:\wamp64\www\myproject\src\vendor\symfony\symfony\src\symfony\Bridge\Twig/Resources/views/Form)。在C:\wamp64\www\myproject\src\vendor\symfony\symfony\src\symfony\Bridge\Twig\TwigEngine.php的第127行

Call Stack:
    0.2183     585024   1. {main}() C:\Users\myuser\AppData\Local\Temp\PHP2E4D.tmp:0
    2.2398    2660672   2. __phpunit_run_isolated_test() C:\Users\myuser\AppData\Local\Temp\PHP2E4D.tmp:606
    2.2411    2756680   3. Tests\AppBundle\Controller\MetierControllerTest->run(???) C:\Users\myuser\AppData\Local\Temp\PHP2E4D.tmp:327
    2.2428    2964136   4. PHPUnit_Framework_TestResult->run(???) C:\wamp64\www\myproject\src\vendor\phpunit\phpunit\src\Framework\TestCase.php:868
    2.2473    3000096   5. Tests\AppBundle\Controller\MetierontrollerTest->runBare() C:\wamp64\www\myproject\src\vendor\phpunit\phpunit\src\Framework\TestResult.php:686
    2.2532    3017024   6. Tests\AppBundle\Controller\MetierControllerTest->runTest() C:\wamp64\www\myproject\src\vendor\phpunit\phpunit\src\Framework\TestCase.php:913
    2.2532    3017320   7. ReflectionMethod->invokeArgs(???, ???) C:\wamp64\www\myproject\src\vendor\phpunit\phpunit\src\Framework\TestCase.php:1062
    2.2532    3017328   8. Tests\AppBundle\Controller\MetierControllerTest->testIndex() C:\wamp64\www\myproject\src\vendor\phpunit\phpunit\src\Framework\TestCase.php:1062
    2.2532    3017328   9. Tests\AppBundle\Controller\MetierControllerTest->checkRegenerateFacture()     C:\wamp64\www\myproject\src\tests\AppBundle\Controller\MetierControllerTest.php:15
    5.0054    5866064  10. Symfony\Bundle\FrameworkBundle\Client->request(???, ???, ???, ???, ???, ???, ???)     C:\wamp64\www\myproject\src\tests\AppBundle\Controller\MetierControllerTest.php:26
    5.0093    5965232  11. Symfony\Bundle\FrameworkBundle\Client->doRequest(???) C:\wamp64\www\myproject\src\vendor\symfony\symfony\src\Symfony\Component\BrowserKit\Client.php:318
    5.0093    5965232  12. Symfony\Bundle\FrameworkBundle\Client->doRequest(???) C:\wamp64\www\myproject\src\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Client.php:131
    5.0093    5965232  13. AppKernel->handle(???, ???, ???) C:\wamp64\www\myproject\src\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\Client.php:68
    5.0326    7010312  14. Symfony\Component\HttpKernel\HttpKernel->handle(???, ???, ???) C:\wamp64\www\myproject\src\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\Kernel.php:200
    5.0327    7011048  15. Symfony\Component\HttpKernel\HttpKernel->handleRaw(???, ???) C:\wamp64\www\myproject\src\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\HttpKernel.php:68
   23.6599   25210184  16. Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher->dispatch(???, ???)     C:\wamp64\www\myproject\src\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\HttpKernel.php:156
   29.2349   25323480  17. Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(???, ???)     C:\wamp64\www\myproject\src\vendor\symfony\symfony\src\Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher.php:143
   29.2350   25323856  18. Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher->doDispatch(???, ???, ???)     C:\wamp64\www\myproject\src\vendor\symfony\symfony\src\Symfony\Component\EventDispatcher\EventDispatcher.php:44
   29.2350   25323856  19. Symfony\Component\EventDispatcher\Debug\WrappedListener->__invoke(???, ???, ???)     C:\wamp64\www\myproject\src\vendor\symfony\symfony\src\Symfony\Component\EventDispatcher\EventDispatcher.php:212
   29.2351   25324416  20. FOS\RestBundle\EventListener\ViewResponseListener->onKernelView(???, ???, ???)     C:\wamp64\www\myproject\src\vendor\symfony\symfony\src\Symfony\Component\EventDispatcher\Debug\WrappedListener.php:115
   29.2360   25354176  21. FOS\RestBundle\View\ViewHandler->handle(???, ???) C:\wamp64\www\myproject\src\vendor\friendsofsymfony\rest-bundle\EventListener\ViewResponseListener.php:132
   29.2360   25354176  22. FOS\RestBundle\View\ViewHandler->createResponse(???, ???, ???) C:\wamp64\www\myproject\src\vendor\friendsofsymfony\rest-bundle\View\ViewHandler.php:310
   29.2360   25354176  23. FOS\RestBundle\View\ViewHandler->initResponse(???, ???) C:\wamp64\www\myproject\src\vendor\friendsofsymfony\rest-bundle\View\ViewHandler.php:426
   29.2360   25354176  24. FOS\RestBundle\View\ViewHandler->renderTemplate(???, ???) C:\wamp64\www\myproject\src\vendor\friendsofsymfony\rest-bundle\View\ViewHandler.php:452
   29.2361   25354552  25. Symfony\Bundle\TwigBundle\TwigEngine->render(???, ???) C:\wamp64\www\myproject\src\vendor\friendsofsymfony\rest-bundle\View\ViewHandler.php:373
   29.2361   25354552  26. Symfony\Bundle\TwigBundle\TwigEngine->render(???, ???) C:\wamp64\www\myproject\src\vendor\symfony\symfony\src\Symfony\Bundle\TwigBundle\TwigEngine.php:45
   29.2361   25354552  27. Symfony\Bundle\TwigBundle\TwigEngine->load(???) C:\wamp64\www\myproject\src\vendor\symfony\symfony\src\Symfony\Bridge\Twig\TwigEngine.php:49

将此添加到config_test.yml文件时解决了问题:

imports:
- { resource: parameters_test.yml }
- { resource: config.yml }

framework:
    test: true

security:
    firewalls:
        # replace 'main' by the name of your own firewall
        main:
            http_basic: ~
fos_rest:
    format_listener:
        rules:
            - { path: '^/api', fallback_format: json }
            - { path: '^/', fallback_format: json }

这是什么样的API?你用的是什么软件包?你的控制器看起来怎么样?