使用数据库访问在 Symfony 4 中测试服务的正确方法
proper way to test a service in Symfony 4, with database access
在同时访问数据库的 Symfony 4 中测试服务的正确方法是什么?
我是 Symfony4 的新手(在我为 Symfony2 开发之前),我想为服务编写我的第一个测试。
此服务是通过数据库中的 Entities / Doctrine / ORM 编写的,我想测试的每个方法是否触发数据库保存。
在 Symfony 2 中,当我使用 KernelTestCase 而不是 PHPUnit_Framework_TestCase 时就是这种情况,因为模拟 EntityManager 是一件很痛苦的事情,而且我经常还想在测试数据库中检查结果。
Symfony 4 的所有示例仅提及用于测试命令的 KernelTestCase。
我的class:
class UserPropertyService implements UserPropertyServiceInterface
{
public function __construct(EntityManager $em, LoggerInterface $logger)
{
....
}
....
}
我的测试尝试:
class UserPropertyServiceTest extends KernelTestCase
{
/** @var UserPropertyService */
private $userPropertyService;
public function setUp()
{
self::bootKernel();
$client = static::createClient();
$container = $client->getContainer();
$this->userPropertyService = self::$container->get('app.user_management.user_property_service');
}
结果:
Cannot autowire service "App\Service\UserManagement\UserPropertyService": argument
"$em" of method "__construct()" references class "Doctrine\ORM\EntityManager"
but no such service exists.
Try changing the type-hint to one of its parents: interface "Doctrine\ORM\EntityManagerInterface",
or interface "Doctrine\Common\Persistence\ObjectManager".
这里的正确方法是什么?我应该使用哪个测试 class?
这是服务测试的样子(不要通过客户端获取容器,那些容器是不同的)
顺便说一下,如果你从 KernelTestCase
扩展你不能使用 static::createClient();
(对控制器测试和 WebTestCase
class 的误解?)
<?php
namespace App\Tests\Service;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
class UserPropertyServiceTest extends KernelTestCase
{
/** @var UserPropertyService */
private $myService;
public function setUp() {
self::bootKernel();
$this->myService = self::$kernel->getContainer()->get('app.user_management.user_property_service');
}
}
在同时访问数据库的 Symfony 4 中测试服务的正确方法是什么?
我是 Symfony4 的新手(在我为 Symfony2 开发之前),我想为服务编写我的第一个测试。
此服务是通过数据库中的 Entities / Doctrine / ORM 编写的,我想测试的每个方法是否触发数据库保存。
在 Symfony 2 中,当我使用 KernelTestCase 而不是 PHPUnit_Framework_TestCase 时就是这种情况,因为模拟 EntityManager 是一件很痛苦的事情,而且我经常还想在测试数据库中检查结果。
Symfony 4 的所有示例仅提及用于测试命令的 KernelTestCase。
我的class:
class UserPropertyService implements UserPropertyServiceInterface
{
public function __construct(EntityManager $em, LoggerInterface $logger)
{
....
}
....
}
我的测试尝试:
class UserPropertyServiceTest extends KernelTestCase
{
/** @var UserPropertyService */
private $userPropertyService;
public function setUp()
{
self::bootKernel();
$client = static::createClient();
$container = $client->getContainer();
$this->userPropertyService = self::$container->get('app.user_management.user_property_service');
}
结果:
Cannot autowire service "App\Service\UserManagement\UserPropertyService": argument
"$em" of method "__construct()" references class "Doctrine\ORM\EntityManager"
but no such service exists.
Try changing the type-hint to one of its parents: interface "Doctrine\ORM\EntityManagerInterface",
or interface "Doctrine\Common\Persistence\ObjectManager".
这里的正确方法是什么?我应该使用哪个测试 class?
这是服务测试的样子(不要通过客户端获取容器,那些容器是不同的)
顺便说一下,如果你从 KernelTestCase
扩展你不能使用 static::createClient();
(对控制器测试和 WebTestCase
class 的误解?)
<?php
namespace App\Tests\Service;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
class UserPropertyServiceTest extends KernelTestCase
{
/** @var UserPropertyService */
private $myService;
public function setUp() {
self::bootKernel();
$this->myService = self::$kernel->getContainer()->get('app.user_management.user_property_service');
}
}