phpUnit无法断言null是类“的实例”;Zend\View\Model\ViewModel";
我不熟悉phpunit测试和Zend框架。我在做一个基本测试时遇到了这个错误:phpUnit无法断言null是类“的实例”;Zend\View\Model\ViewModel";,php,doctrine-orm,zend-framework2,phpunit,Php,Doctrine Orm,Zend Framework2,Phpunit,我不熟悉phpunit测试和Zend框架。我在做一个基本测试时遇到了这个错误: Failed asserting that null is an instance of class "Zend\View\Model\ViewModel" 我的测试如下所示: public function testFooActionCanBeAccessed() { $mockView = $this->getMockBuilder('pathTo\Model\View\MyView',
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();
}
正在测试的方法如下所示:
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();
}
我的设置如下所示:
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();
}
那么,我在这里遗漏了什么呢?方法通过设计返回null。它只是调用页面,类似于在浏览器中查看页面时所做的操作。它不返回视图模型 在测试中,您希望检查视图中的实际内容。使用
$this->getResponse()
并检查返回的响应的状态。响应还将有一个getContent()
方法,您可以使用该方法检查返回的html或json字符串,并确保在视图中显示正确的数据
将控制器测试视为在浏览器中打开页面。你会在页面上寻找什么来确保工作正常
另外:
在测试中,您没有在任何地方注入mockUser,因此您的测试仍将失败,因为您的
$mockView
不会像您预期的那样使用$mockUser
调用。您需要将with()
参数更改为ZfcUser\Entity\User
的实例,这是您在设置中实际获得的方法,或者修改测试,以便您的$mockUser
是实际从$this->zfcUserAuthentication()返回的->getIdentity()代码>谢谢您的回答!我用getContent检查html,现在它给了我404。你能给我举个注射的例子吗?文件可以在