我正在将项目迁移到Symfony4。在我的测试套件中,我们使用PHPUnit进行功能测试(我的意思是,我们调用端点,然后检查结果)。通常,我们模拟服务以检查不同的步骤。
自从我迁移到Symfony 4以来,我面临着这个问题:Symfony\Component\DependencyInjection\Exception\InvalidArgumentException: The "my.service" service is already initialized, you cannot replace it.
当我们像这样重新定义它时:static::$container->set("my.service", $mock);
仅用于测试,如何解决此问题?
谢谢
我正在尝试将我的 Symfony 3.4 应用程序迁移到 Symfony 4.1。
测试不起作用,因为这些服务默认是私有的(这是一个好消息)。
在这篇文章之后:https://symfony.com/blog/new-in-symfony-4-1-simple-service-testing,我仍然面临着私人服务的问题:
Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException:在编译容器时“my.service”服务或别名已被删除或内联。您应该将其公开,或者直接停止使用容器并改用依赖注入。
在以下编译器传递中,未在以下位置找到我的私人服务$definitions:vendor/symfony/framework-bundle/DependencyInjection/Compiler/TestServiceContainerRealRefPass.php
应该是什么问题?
更新
这里的定义:
<service id="my.service"
class="My\Bundle\GreatService">
<argument type="service" id="doctrine.orm.entity_manager" />
</service>
Run Code Online (Sandbox Code Playgroud)
更新(再次)
这里是单元测试
<?php
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
class MyServiceTest extends KernelTestCase
{
protected $myService;
public function setUp()
{
self::bootKernel();
$this->myService = self::$container->get('my.service');
}
//...
}
Run Code Online (Sandbox Code Playgroud)