【发布时间】:2021-02-01 14:26:10
【问题描述】:
我正在为typo3 v9.5 扩展编写一些单元测试,当被测函数使用GeneralUtility::makeInstance(ObjectManager::class)->get(...) 实例化对象时,我不知道如何正确模拟对象
如果可能,我想使用the prophecy framework,但这不是强制性的。
例如,如果被测函数是这样的:
public function getRootline($pageUid) {
$pageService = GeneralUtility::makeInstance(ObjectManager::class)->get(PageService::class);
return $pageService->getRootLine($pageUid);
}
如何测试并模拟 Pageservice 类?
我尝试了这个解决方案的不同变体:
$pageServiceProphecy = $this->prophesize(PageService::class);
$pageServiceProphecy->getRootLine($pageUid)->shouldBeCalled()->willReturn($myRootLine);
$omProphecy = $this->prophesize(ObjectManager::class);
$omProphecy->get(PageService::class)->shouldBeCalled()->willReturn($pageServiceProphecy);
GeneralUtility::setSingletonInstance(ObjectManager::class, $omProphecy->reveal());
但我每次都会遇到各种神秘的错误。 给定版本引发错误:
TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException: A cache with identifier "cache_runtime" does not exist.
【问题讨论】:
标签: php unit-testing typo3 typo3-9.x