1

我已经构建了ZF2“入门”应用程序,并尝试将ZF2 UnitTesting 教程中的测试应用到它。现在我在这个简单的测试中遇到了麻烦:

[项目]/module/Album/test/AlbumTest/Controller/AlbumControllerTest.php

<?php
namespace AlbumTest\Controller;
...
class AlbumControllerTest extends AbstractHttpControllerTestCase
{
    ...
    public function testIndexActionCanBeAccessed()
    {
        $this->dispatch('/album');
        $this->assertResponseStatusCode(200);

        $this->assertModuleName('Album');
        $this->assertControllerName('Album\Controller\Album');
        $this->assertControllerClass('AlbumController');
        $this->assertMatchedRouteName('album');
    }
}

命令行:/var/www/sandbox/zf2sandbox/module/Album/test/

# phpunit
PHPUnit 3.7.13 by Sebastian Bergmann.

Configuration read from /var/www/sandbox/zf2sandbox/module/Album/test/phpunit.xml

F....

Time: 0 seconds, Memory: 10.00Mb

There was 1 failure:

1) AlbumTest\Controller\AlbumControllerTest::testIndexActionCanBeAccessed
Failed asserting response code "200", actual status code is "500"

/var/www/sandbox/zf2sandbox/vendor/zendframework/zendframework/library/Zend/Test/PHPUnit/Controller/AbstractControllerTestCase.php:373
/var/www/sandbox/zf2sandbox/module/Album/test/AlbumTest/Controller/AlbumControllerTest.php:49

FAILURES!
Tests: 5, Assertions: 11, Failures: 1.

所以,assertResponseStatusCode(200)失败了,因为状态码是500为什么?

当我在浏览器( http://zf2sandbox.sandbox.loc/album )中调用此路径时,它可以工作:

在此处输入图像描述

谢谢

4

1 回答 1

3

答案就在您遵循的教程的下一个标题中。一个失败的测试用例。

所以根据它,你需要配置服务管理器。

$serviceManager = $this->getApplicationServiceLocator();
$serviceManager->setAllowOverride(true);
$serviceManager->setService('Album\Model\AlbumTable', $albumTableMock);

此外,添加这个对于在单元测试中获得正确的错误非常有帮助,如链接中所述。

protected $traceError = true;
于 2013-02-26T13:11:40.360 回答