问题描述
我正在关注 2.1 版的 Zend Framework 2 官方教程.在 单元测试 部分,我应该在其中运行 phpunit模块/应用程序/测试我遇到了以下问题:
I am following the official Zend Framework 2 tutorial for version 2.1. In the Unit Testing section, where I am supposed to run phpunit in module/Application/test I am running into the following problem:
user@xubuntu1210:~/Desktop/zf2-tutorial/module/Application/test$ phpunit
PHPUnit 3.7.13 by Sebastian Bergmann.
Configuration read from /home/user/Desktop/zf2-tutorial/module/Application/test/phpunit.xml.dist
E
Time: 0 seconds, Memory: 4.00Mb
There was 1 error:
1) ApplicationTest\Controller\IndexControllerTest::testIndexActionCanBeAccessed
Zend\ModuleManager\Exception\RuntimeException: Module (Application) could not be initialized.
/home/user/Desktop/zf2-tutorial/vendor/zendframework/zendframework/library/Zend/ModuleManager/ModuleManager.php:140
/home/user/Desktop/zf2-tutorial/vendor/zendframework/zendframework/library/Zend/ModuleManager/ModuleManager.php:81
/home/user/Desktop/zf2-tutorial/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php:460
/home/user/Desktop/zf2-tutorial/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php:204
/home/user/Desktop/zf2-tutorial/vendor/zendframework/zendframework/library/Zend/ModuleManager/ModuleManager.php:100
/home/user/Desktop/zf2-tutorial/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php:239
/home/user/Desktop/zf2-tutorial/vendor/zendframework/zendframework/library/Zend/Test/PHPUnit/Controller/AbstractControllerTestCase.php:146
/home/user/Desktop/zf2-tutorial/vendor/zendframework/zendframework/library/Zend/Test/PHPUnit/Controller/AbstractControllerTestCase.php:173
/home/user/Desktop/zf2-tutorial/vendor/zendframework/zendframework/library/Zend/Test/PHPUnit/Controller/AbstractControllerTestCase.php:193
/home/user/Desktop/zf2-tutorial/vendor/zendframework/zendframework/library/Zend/Test/PHPUnit/Controller/AbstractControllerTestCase.php:236
/home/user/Desktop/zf2-tutorial/module/Application/test/ApplicationTest/Controller/IndexControllerTest.php:20
FAILURES!
Tests: 1, Assertions: 0, Errors: 1.
我从教程中复制了 IndexControllerTest.php 的内容.
I copied the contents of IndexControllerTest.php from the tutorial.
<?php
namespace ApplicationTest\Controller;
use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;
class IndexControllerTest extends AbstractHttpControllerTestCase
{
public function setUp()
{
$this->setApplicationConfig(
include '/home/user/Desktop/zf2-tutorial/config/application.config.php'
);
parent::setUp();
}
public function testIndexActionCanBeAccessed()
{
$this->dispatch('/'); // this is line 20
$this->assertResponseStatusCode(200);
$this->assertModule('application');
$this->assertControllerName('application_index');
$this->assertControllerClass('IndexController');
$this->assertMatchedRouteName('home');
}
}
我不知道为什么应用程序不会初始化,我希望得到任何指点.
I have no clue why the application won't initialize and I would appreciate any pointers.
推荐答案
这是一个自动加载问题,可能与 config/application.config.php
中的 module_paths
选项有关code>(在本教程中,它是您在 /path/to/application/config/test/application.config.php
中的那个).
This is an autoloading problem that may be related with the module_paths
option in config/application.config.php
(in the tutorial, it's the one you have in /path/to/application/config/test/application.config.php
).
那个 application.config.php
可能如下所示:
That application.config.php
probably looks like following:
return array(
'modules' => array(
'Application',
'Album',
),
'module_listener_options' => array(
'module_paths' => array(
'./module',
'./vendor',
),
),
);
要解决您的问题,请将值 ./module
更改为绝对路径,例如 __DIR__ .'/../module
,假设 application.config.php
位于应用程序根目录的 config/
中.
To fix your problem, change the value ./module
into an absolute path, for example __DIR__ . '/../module
, assuming the application.config.php
is in config/
in your application's root dir.
澄清:出现问题是因为 ./module
是相对于您的 cwd
的路径.运行 phpunit
时,您的 cwd
位于测试目录中,其中(显然)没有任何 module
目录.
To clarify: the problem occurs because ./module
is a path relative to your cwd
. Your cwd
when running phpunit
is inside the test dir, where there isn't (obviously) any module
dir.
这篇关于Zend Framework 2 教程:无法初始化模块(应用程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!