我正在为Symfony2创建一个小包,它只提供服务;可以调用它来在数据库中插入一行,其中包含一些参数:
class AuditLogService {
private $em;
public function __construct(EntityManager $em)
{
$this->em = $em;
}
public function log($type, $channel, $message) {
$log = new AuditLog();
$log->setType($type);
$log->setChannel($channel);
$log->setMessage($message);
$this->em->persist($log);
$this->em->flush();
}
}
现在我需要为它编写一个测试,所以基本上这里是我应该采取的步骤:
编写一个获取记录器服务的测试,但改为使用模拟entityManager
?
使用模拟的em
尝试使用我在步骤2中使用的参数从模拟的em中取回
断言我是否找到了结果
我非常确定理论上这些是正确的步骤,但是有人能指出我如何实际做到正确的方向吗?
我有这个:
public function testServiceLoaded()
{
$this->assertEquals(get_class($this->container->get('bbit_audit_log.service')), 'BBIT\AuditLogBundle\Services\AuditLogService');
}
返回时出现以下错误:
" bbit_audit_log.service"依赖于不存在的服务" doctrine.orm.entity_manager"
好像我的容器不包含entitymanager
?
答案 0 :(得分:1)
在单元测试中跳过3和4. AuditLogService不负责保存在数据库中(但要调用em)。 3和4应通过功能测试进行测试。