2012-06-01 49 views
2

我想写一些测试用例,我正在写一个ZF应用程序,但我似乎无法通过这一点。Zend自动加载模型在PHPUnit

我已经扩展了Zend_Test_PHPUnit_ControllerTestCase,增加以下内容:

public function setUp() { 
    $this->bootstrap = new Zend_Application('testing', 
     APPLICATION_PATH . '/configs/application.ini'); 
    parent::setUp(); 
} 

它的工作原理,以及它初始化了Zend自动加载磁带机,让我加载我的图书馆以及Zend类内的其他类。所以我设置了一个测试:

public function testUserUnauthorized() { 
    $this->dispatch('/api/user'); 
    //Assertions... 
} 

不幸的是,测试永远不会通过调度。相反,它给我一个错误:

Fatal error: Class 'App_Model_User' not found in ....Action.php 

action.php的是扩展Zend_Controller_Action类。其中有一项功能使用App_Model_User来验证登录的用户。自从自动装载机工作以来,我从不必添加require_once。这是奇怪的部分:它通过我的浏览器工作。只是不在PHPUnit中。

所以我甩使用Zend自动加载机注册的自动加载磁带机:

public function testUserUnauthorized() { 
    print_r(Zend_Loader_Autoloader::getInstance()->getAutoloaders()); 
    exit; 
    $this->dispatch('/api/user'); 
} 

它显示我的所有模块,以及其命名空间视图,控制器,模型等我做了同样的通过我的浏览器垃圾箱匹配。

+0

缺少自动加载磁带机的配置?一些自定义spl自动加载器?找出哪个自动加载器加载了App_Model_User类。 – MonkeyMonkey

回答

1

Zend的自动装弹机需要APP_添加为自动加载前缀这是你期望的那样,像这样的工作:

$autoloader = Zend_Loader_Autoloader::getInstance(); 
$autoloader->registerNamespace("App_"); 
+0

我会试试这个。谢谢。 –

+0

没有快乐。似乎没有效果。 –

+1

这解决了困扰我一天中很大一部分的问题:谢谢!我遇到的部分问题是许多与ZF(1)和PHPUnit有关的教程至少有4年的历史,并且早于Zend_Loader_Autoloader的更新版本。您的解决方案适用于ZF 1.12.3。 –