我开始为我的扩展编写一些UnitTests,现在我遇到了这个错误的问题:
Call to a member function hasArgument() on a non-object in C:\xampp\htdocs\typo3\typo3conf\ext\rere\Classes\Controller\FachController.php on line 121
FachController的第121行是:
if ($this->request->hasArgument('modul')) {
我认为问题在于我没有将Argument发送给控制器。
所以我尝试了这个:
$fach = new \ReRe\Rere\Domain\Model\Fach();
$view = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\View\\ViewInterface');
$view->expects($this->once())->method('assign')->with(array('newFach' => $fach, 'modul' => 1));
$this->inject($this->subject, 'view', $view);
$this->subject->newAction($fach);
但那没有任何影响......为什么?
更新
现在不能正常工作......试图为其他元素添加更多的模拟......比如findByUid ......但模拟应该怎么样?
$modul = new \ReRe\Rere\Domain\Model\Modul();
$mockRequest = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Request');
$mockRequest->expects($this->once())->method('hasArgument')->with('modul');
$this->inject($this->subject, 'request', $mockRequest);
$mockRequest->expects($this->once())->method('findByUid')->with(1);
$this->inject($this->subject, 'request', $mockRequest);
$view = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\View\\ViewInterface');
$this->inject($this->subject, 'view', $view);
//$view->expects($this->once())->method('assign')->with('modul', $modul);
$this->subject->showAction();
更新3
新操作代码因getUid()
而出错 public function newActionAssignsTheGivenFachToView() {
$fach = new \ReRe\Rere\Domain\Model\Fach();
$mockRequest = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Request');
$mockRequest->expects($this->any())->method('hasArgument')->with('modul');
$this->inject($this->subject, 'request', $mockRequest);
$objectmanager = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\ObjectManager', array(), array(), '', FALSE);
$this->inject($this->subject, 'objectManager', $objectmanager);
$modulRepository = $this->getMock('ReRe\\Rere\\Domain\\Repository\\ModulRepository');
$modulRepository->expects($this->any())->method('findByUid')->with(1);
$this->inject($this->subject, 'modulRepository', $modulRepository);
$view = $this->getMock(self::VIEWINTERFACE);
$view->expects($this->any())->method(self::ASSIGN)->with('newFach', $fach);
$this->inject($this->subject, 'view', $view);
$this->subject->newAction($fach);
}
答案 0 :(得分:2)
您的测试失败,因为$this->request
的单元测试不知道newAction
。您需要为请求创建一个mockObject,并将其注入newAction
的测试中,如下所示:
$mockRequest = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Request');
$mockRequest->expects($this->once())->method('hasArgument')->with('modul');
$this->inject($this->subject, 'request', $mockRequest);