如何模拟您正在使用 Prophecy 测试的类中的方法?

问题描述 投票:0回答:3

我想第一次使用 Prophecy (“phpspec/prophecy-phpunit”)为我的类创建单元测试。我想测试一个调用同一服务中另一个函数的函数,代码如下:

class UserManager
{
    private $em;
    private $passwordHelper;

    public function __construct(\Doctrine\ORM\EntityManager $em, \MainBundle\Helper\PasswordHelper $passwordHelper)
     {
         $this->em = $em;
         $this->passwordHelper = $passwordHelper;
     }

     public function getUserForLdapLogin($ldapUser)
     {
          $dbUser = $this
              ->em
              ->getRepository('MainBundle:User')
              ->findOneBy(array('username' => $ldapUser->getUsername()));

         return (!$dbUser) ?
              $this->createUserFromLdap($ldapUser) :
              $this->updateUserFromLdap($ldapUser, $dbUser);
     }

我遇到的第一个问题是我正在使用

findOneByUsername
和预言,据我所知,不允许你:模拟魔法方法(
_call
代表
EntityRepository
),模拟不存在的方法,模拟您正在测试的课程。如果这些是真的,我就有点麻烦了,这意味着我无法在不测试类中其他函数的情况下测试这个函数。

到目前为止,我的测试如下:

class UserManagerTest extends \Prophecy\PhpUnit\ProphecyTestCase
{

      public function testGetUserForLdapLoginWithNoUser()
      {
          $ldapUser = new LdapUser();
          $ldapUser->setUsername('username');

          $em = $this->prophesize('Doctrine\ORM\EntityManager');
          $passwordHelper = $this->prophesize('MainBundle\Helper\PasswordHelper');

          $repository = $this->prophesize('Doctrine\ORM\EntityRepository');
          $em->getRepository('MainBundle:User')->willReturn($repository);
          $repository->findOneBy(array('username' => 'username'))->willReturn(null);

          $em->getRepository('MainBundle:User')->shouldBeCalled();
          $repository->findOneBy(array('username' => 'username'))->shouldBeCalled();

          $service = $this->prophesize('MainBundle\Helper\UserManager')
            ->willBeConstructedWith(array($em->reveal(), $passwordHelper->reveal()));

          $service->reveal();
          $service->getUserForLdapLogin($ldapUser);
     }
}

当然,测试会失败,因为

$em
上的承诺和存储库未实现。如果我实例化正在测试的类,测试会失败,因为该函数随后在同一个类上调用
createUserFromLdap()
并且未进行测试。

有什么建议吗?

php symfony phpunit prophecy
3个回答
2
投票

您想要实现的是部分模拟,Prophecy 不支持它。有关更多信息,请参见此处https://github.com/phpspec/prophecy/issues/101https://github.com/phpspec/prophecy/issues/61

TL;DR; 设计你的类时要牢记单一职责,这样你就不必模拟其他功能。


1
投票

第一个问题:

不要使用魔法,魔法是邪恶的。 __call 可能会导致不可预测的行为。

“$em 和存储库上的承诺没有兑现”:

不要让你的代码依赖于类而是接口。 然后模拟接口而不是类! 您应该模拟 ObjectManager 而不是 EntityManager。 (不要忘记更改参数的类型)

最后一点:

揭晓之前。

$service->createUserFromLdap()
   ->shouldBeCalled()
   ->willReturn(null);

0
投票

关于无法模拟不存在的方法的问题,您可以使用

http://docs.mockery.io/en/latest/

代替预言。嘲笑可以让你做到这一点。严格来说,这确实违反了一些良好设计的规则,但另一方面,有时它非常有用。无论如何,就功能而言,mockery 非常相似,而且在我看来它同样直观且易于使用。但是,他们还没有发布稳定版本,所以如果您决定使用它,请注意这一点。

在这里您可以找到两个库的良好比较:

嘲讽与预言的概念差异

© www.soinside.com 2019 - 2024. All rights reserved.