我想在我的单元测试中注入ParameterBagInterface和EntityManagerInterface (WebTestCase和KernelTestCase),但是我找不到一个能够正确返回它们的命名空间和名称的方法(Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface)。有没有办法做到这一点?
我试过的是:
$this->parameterBag = self::$container->get(ParameterBagInterface::class);
返回Symfony\Component\DependencyInjection\ParameterBag\ContainerBag
$this->parameterBag = $this->prophesize(ParameterBagInterface::class)->reveal();
返回Double\ParameterBagInterface\P1
$this->parameterBag = $this->createMock(ParameterBagInterface::class);
返回Mock_ParameterBagInterface_fccf09f9
我的所有类都在使用ParameterBagInterface,并且都是类型提示的。
下面是测试类示例:
/**
*
* @package App\Tests\Entity
*/
class LogCollectTest extends WebTestCase
{
use CronManagerCron;
/**
* @var EntityManager
*/
private $em;
/**
* {@inheritDoc}
*/
protected function setUp()
{
self::bootKernel();
$this->parameterBag = self::$container->get(ParameterBagInterface::class);
}
/**
* Test saving click
*/
public function testSavingClick()
{
// truncate the log collect table to be sure to get the right click
$this->truncateLogCollectTable();
$userAgents = [...];
foreach ($userAgents as $agent => $expectedResult) {
// we make fake client requests and record them in database (test enviropment)
$clientStatus = $this->sendClientData($agent);
// the controller is resulting properly
$this->assertEquals(200, $clientStatus);
/**
* @var LogCollect $logCollectEntry
*/
$logCollectEntry = $this->em->getRepository(LogCollect::class)->getLast(); <--
...
// later we process this client requests with cron and later assert the data
$logCollectorCron = new LogCollectorCron(
$this->container,
$this->em,
$this->parameterBag,
'test'
);
$logCollectorCron->run();
...
}有什么建议吗?
发布于 2020-09-13 04:03:56
你不会得到任何接口,因为接口本质上是永远不能实例化的,所以需要说明的是: ParameterBagInterface对象不能存在。当您请求容器为您提供ParameterBagInterface时,容器将为您提供一个实现此接口的服务。
https://stackoverflow.com/questions/63863485
复制相似问题