Symfony - PHPUnit Prophecy 不触发调用

Symfony - PHPUnit Prophecy not triggering calls

我正在尝试使用 Symfony 和 phpunit 进行最简单的 prophecy 测试。

如果我在 persist 方法中放置一个断点,我可以验证它是否通过了它。

        $em = $this->prophesize(EntityManagerInterface::class);
        $em->persist(Argument::any())->shouldBeCalledTimes(1);
       

        $realEm = self::$container->get('doctrine.orm.entity_manager');
        $realEm->persist($contract->getSensorConfiguration());

但它总是说我从未被调用过。

Some predictions failed:
Double\EntityManagerInterface\P1:
  Expected exactly 1 calls that match:
      Double\EntityManagerInterface\P1->persist(*)
    but none were made.

我也试过 createMockexpects

        $em = $this->createMock(EntityManager::class);
        $em->expects($this->once())->method('persist');

但我得到了相同的结果...

Expectation failed for method name is equal to 'persist' when invoked 1 time(s).
Method was expected to be called 1 times, actually called 0 times.

您似乎希望 $em 与您从容器中获取的 entitymanager 相同。但这些是完全不同的对象。

我不推荐它,但你可以先做 self::$container->set('doctrine.orm.entity_manager', $em)(未测试)。所以现在每个使用此服务的 class 都会使用你的模拟。