在Symfony 3.4中,我使用这种方法来获取测试类中的条令实体:
测试类(代码段(:
$kernel = self::bootKernel();
$em = $kernel->getContainer()
->get('doctrine')
->getManager();
这是不赞成的。
我正试图将实体管理器注入我的测试中,如下所示:
服务.yml
TestsAppBundleNewUserTest:
public: true
autowire: true
calls:
- [ setEntityManager, ['@doctrine.orm.entity_manager']]
测试类(snippet,namespace="Tests\AppBundle"(:
/**
* @param EntityManager $em
*/
public function setEntityManager(EntityManager $em)
{
self::$em = $em;
}
我不能使用构造函数注入进行注入(因为WebTestCases需要一堆我无法访问的构造函数参数(
有人能帮我一下吗?我一直在到处寻找这个问题,但没有解决方案。有一些类似的问题,但不是在测试环境中。
谢谢:(
我认为你可以为它创建客户端。客户端拥有你需要的所有服务。
namespace AppTestsController;
use SymfonyBundleFrameworkBundleTestWebTestCase;
class WebTest extends WebTestCase
{
private $client;
public function setUp()
{
$this->client = static::createClient();
}
public function getLoggedInClient()
{
$session = $this->client->getContainer()->get('session');
}
}