带有多个参数的 Typo3 PHPUNIT 测试

Typo3 PHPUNIT tests with multiple arguments

我开始为我的扩展编写一些单元测试,现在我遇到了这个错误的问题:

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')) {

我认为问题是我没有将参数发送到控制器。

所以我尝试了这个:

    $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);
}

您的测试失败,因为 $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);