【发布时间】:2018-07-14 12:14:21
【问题描述】:
这是扩展 BaseEntityManager 的部分代码:
namespace Vop\PolicyBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Persistence\ObjectRepository;
use Sonata\CoreBundle\Model\BaseEntityManager;
class AdditionalInsuredTypeManager extends BaseEntityManager
{
/**
* @param int $productId
*
* @return ArrayCollection
*/
public function getProductInsuredTypes($productId = null)
{
$repository = $this->getRepository();
$allActiveTypes = $repository->findAllActive();
// other code
}
/**
* @return AdditionalInsuredTypeRepository|ObjectRepository
*/
protected function getRepository()
{
return parent::getRepository();
}
}
在这里我正在尝试编写一个单元测试:
public function testGetProductInsuredTypes()
{
$managerRegistry = $this->getMockBuilder(\Doctrine\Common\Persistence\ManagerRegistry::class)
->getMock();
$additionalInsuredTypeManager = new AdditionalInsuredTypeManager(
AdditionalInsuredTypeManager::class,
$managerRegistry
);
$additionalInsuredTypeManager->getProductInsuredTypes(null);
}
有什么问题:
- 我在嘲笑 ManagerRegistry,但我知道我不应该嘲笑我不拥有的东西。但这是构造函数的必需参数。
- 我收到错误:
找不到 Vop\PolicyBundle\Entity\AdditionalInsuredTypeManager 类的映射信息。请检查“auto_mapping”选项 (http://symfony.com/doc/current/reference/configuration/doctrine.html#configuration-overview) 或将捆绑包添加到原则配置中的“映射”部分。 /home/darius/PhpstormProjects/vop/vendor/sonata-project/core-bundle/Model/BaseManager.php:54 /home/darius/PhpstormProjects/vop/vendor/sonata-project/core-bundle/Model/BaseManager.php:153 /home/darius/PhpstormProjects/vop/src/Vop/PolicyBundle/Entity/AdditionalInsuredTypeManager.php:46 /home/darius/PhpstormProjects/vop/src/Vop/PolicyBundle/Entity/AdditionalInsuredTypeManager.php:21 /home/darius/PhpstormProjects/vop/src/Vop/PolicyBundle/Tests/Unit/Entity/AdditionalInsuredTypeManagerTest.php:22
我不知道如何修复这个错误,但这确实与我假设的扩展 BaseEntityManager 相关。
我看到错误是由这一行引起的:
$repository = $this->getRepository();
我什至不能从构造函数中注入存储库,因为父构造函数没有这样的参数。
关于测试的信息很少:
https://sonata-project.org/bundles/core/master/doc/reference/testing.html
【问题讨论】:
标签: unit-testing symfony sonata