Phpunit在Symfony 3.4上,您请求的服务不存在

时间:2019-03-01 14:57:24

标签: symfony phpunit

我正在研究从版本2升级的Symfony 3.4安装。

当我尝试使用此命令运行单元测试

./bin/phpunit tests/MyBundle/ -c tests/phpunit.xml

我收到以下错误:

    Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: You have requested a non-existent service "XXXX\MyBundle\Services\ListServices".

/apps/xxxx/unitTest/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Container.php:348
/apps/xxxx/unitTest/tests/MyBundle/Services/ListServicesTest.php:19

我已将服务公开

  services:
    _defaults:
      autowire: true
      autoconfigure: true
      public: true

但是那没有用。我还尝试添加一个Compiler pass,但是那是蠕虫病毒的唯一原因……

我的phpunit.xml

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
                   backupGlobals="false"
                   colors="true"
                   convertErrorsToExceptions   = "true"
                   convertNoticesToExceptions  = "true"
                   convertWarningsToExceptions = "true"
                   processIsolation            = "false"
                   stopOnFailure               = "false"
                   syntaxCheck                 = "false"
                   stderr                      = "true"
                   bootstrap="bootstrap_test.php"
>
<php>
    <ini name="error_reporting" value="-1" />
    <server name="KERNEL_CLASS" value="AppKernel" />
    <server name="KERNEL_DIR" value="../app/" />
    <env name="SYMFONY_DEPRECATIONS_HELPER" value="weak"/>
    <env name="APP_ENV" value="test"/>
</php>


<testsuites>
    <testsuite name="Project Test Suite">
        <directory>../tests/*</directory>
    </testsuite>
</testsuites>

<filter>
    <whitelist>
        <directory>../src</directory>
        <exclude>
            <directory>../src/*/*Bundle/Resources</directory>
            <directory>../src/*/*Bundle/Tests</directory>
            <directory>../src/*/*Bundle/DataFixtures</directory>
            <directory>../src/*/*Bundle/Admin</directory>
            <directory>../src/*/*Bundle/DependencyInjection</directory>
            <directory>../src/*/*Bundle/Twig</directory>
        </exclude>
    </whitelist>
</filter>
</phpunit>

这是我的测试文件

namespace Tests\MyBundle\Services;

use XXXX\MyBundle\Services\ListServices;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class ListServicesTest extends WebTestCase
{
    /**
     * @test
     */    
public function testGetCurrentList()
    {
        $kernel = new AppKernel('dev', false);
        $kernel->boot();
        $container = $kernel->getContainer();

        $listService = $container->get(ListServices::class);

        $this->assertNotNull($listService->getCurrentList());

    }
}

关于如何使单元测试生效的任何想法? 谢谢

1 个答案:

答案 0 :(得分:0)

您正在尝试通过名称(类名)而不是通过容器的别名来获取服务。服务应在容器中注册:

services:
    your_service_alias:
      class: YourApp\Services\ListServices
      arguments: (if any)
        - "@some_dependency"

然后在测试中,您应该通过别名获得服务

$container->get('your_service_alias')