phpUnit断言null是类“Zend ViewModel viewmodel”的实例失败


phpUnit fails asserting that null is an instance of class "Zend ViewModelViewModel"

我是php测试和Zend框架的新手。在做基本测试时,我一直得到这个错误:

Failed asserting that null is an instance of class "Zend'View'Model'ViewModel"

我的测试是这样的:

public function testFooActionCanBeAccessed()
{
    $mockView = $this->getMockBuilder('pathTo'Model'View'MyView', 
    array('getFooView'))
                    ->disableOriginalConstructor()
                    ->getMock();
    $mockUser = $this->getMockBuilder('pathTo'Entity'User')
                    ->disableOriginalConstructor()
                    ->getMock();
    $view = new ViewModel(array());
    $mockView->expects($this->once())
            ->method('getFooView')
            ->with($mockUser)
            ->will($this->returnValue($view));
    $this->routeMatch->setParam('action', 'foo');
    $result = $this->dispatch('/mycontroller/foo');
    $response = $this->controller->getResponse();
    $this->assertEquals(200, $response->getStatusCode());
    $this->assertInstanceOf('Zend'View'Model'ViewModel', $result);
}

正在测试的方法如下所示:

public function fooAction() {
    $user = $this->zfcUserAuthentication()->getIdentity();
    $myView = $this->getServiceLocator()->get('pathTo'Model'View'myView');
    $view = $myView->getFooView($user);
    return $view;
}

我的设置是这样的:

public function setUp()
{
        $serviceManager = Bootstrap::getServiceManager();
        $this->controller = new MyController();
        $this->request    = new Request();
        $this->response    = new Response;
        $this->routeMatch = new RouteMatch
        (array('controller' =>  'pathTo'Controller'My'));
        $this->event      = new MvcEvent();
        $config = $serviceManager->get('Config');
        $this->event->setRouteMatch($this->routeMatch);
        $this->controller->setEvent($this->event);
        $this->controller->setServiceLocator($serviceManager);
        $this->setApplicationConfig(
        include '/pathTo/config/testing.php');
        $mockAuth = $this->getMock('ZfcUser'Entity'UserInterface');
        $ZfcUserMock = $this->getMock('ZfcUser'Entity'User');  
        $ZfcUserMock->expects($this->any())
                    ->method('getId')
                    ->will($this->returnValue('1'));
        $authMock = $this->getMock('ZfcUser'Controller'Plugin'ZfcUserAuthentication');
        $authMock->expects($this->any())
                ->method('hasIdentity')
                -> will($this->returnValue(true));  
        $authMock->expects($this->any())
                ->method('getIdentity')
                ->will($this->returnValue($ZfcUserMock));
        $this->controller->getPluginManager()
        ->setService('zfcUserAuthentication', $authMock);

        $this->em = $this->getMock('EntityManager', array('persist', 'flush'));
        $this->em
            ->expects($this->any())
            ->method('persist')
            ->will($this->returnValue(true));
        $this->em
            ->expects($this->any())
            ->method('flush')
            ->will($this->returnValue(true));
        $this->doctrine = $this->getMock('Doctrine', array('getEntityManager'));
        $this->doctrine
            ->expects($this->any())
            ->method('getEntityManager')
            ->will($this->returnValue($this->em));
        parent::setUp();
}

我在这里错过了什么?

根据设计,dispatch方法返回null。它只是调用页面,类似于您在浏览器中查看页面时所做的事情。它不返回视图模型。

https://github.com/zendframework/zend-test/blob/master/test/PHPUnit/Controller/AbstractControllerTestCaseTest.php

在您的测试中,您想要检查视图中的实际内容。使用$this->getResponse()并检查返回的响应的状态。响应还将有一个getContent()方法,您可以使用它来检查返回的html或json字符串,并确保正确的数据显示在视图中。

将控制器测试视为在浏览器中打开页面。你会在页面上寻找什么来确保一切正常工作?

在您的测试中,您没有在任何地方注入mockUser,因此您的测试仍然会失败,因为您的$mockView不会像您期望的那样被$mockUser调用。您需要将with()参数更改为ZfcUser'Entity'User的实例,这看起来就像您在setUp方法中实际获得的一样,或者修改您的测试,以便您的$mockUser是实际从$this->zfcUserAuthentication()->getIdentity();

返回的那个