我是phpunit-testing和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;
}
我的setUp如下所示:
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();
}
那我在这里错过了什么?
答案 0 :(得分:1)
dispatch
方法按设计返回null。它只是调用页面,类似于您在浏览器中查看页面时所执行的操作。它没有返回视图模型。
在测试中,您想要检查视图中的实际内容。使用$this->getResponse()
并检查返回的响应的状态。响应还将有一个getContent()
方法,您可以使用该方法检查返回的html或json字符串,并确保在视图中显示正确的数据。
像对待浏览器中的页面一样对待控制器测试。你会在页面上寻找什么以确保一切正常?
<强> ALSO: 强>
在您的测试中,您没有在任何地方注入mockUser,因此您的测试仍然会失败,因为$mockView
将不会像您期望的那样使用$mockUser
调用。您需要将with()
参数更改为ZfcUser\Entity\User
的实例,这实际上是您实际使用setUp
方法或修改测试以便$mockUser
1}}是实际从$this->zfcUserAuthentication()->getIdentity();