gpt4 book ai didi

php - Zend 自动加载 PHPUnit 中的模型

转载 作者:搜寻专家 更新时间:2023-10-31 21:41:14 24 4
gpt4 key购买 nike

我正在尝试为我正在编写的 ZF 应用程序编写一些测试用例,但我似乎无法超越这一点。

我扩展了 Zend_Test_PHPUnit_ControllerTestCase,添加了以下内容:

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

它起作用了,它初始化了 Zend Autoloader,允许我在我的库中加载其他类以及 Zend 类。所以我设置了一个测试:

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

可悲的是,测试从未通过 dispatch 。相反,它给了我一个错误:

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

Action.php 是一个扩展 Zend_Controller_Action 的类。其中,有一个函数使用 App_Model_User 对登录用户进行身份验证。自从 Autoloader 工作以来,我从来不需要添加 require_once。这是奇怪的部分:它通过我的浏览器工作。只是不在 PHPUnit 中。

所以我转储了使用 Zend Autoloader 注册的 Autoloader:

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

它显示了我所有的模块,以及 View 、 Controller 、模型等的命名空间。我通过浏览器做了同样的事情,并且转储匹配。

最佳答案

Zend Autoloader 需要将 App_ 添加为自动加载前缀才能按预期工作,如下所示:

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

关于php - Zend 自动加载 PHPUnit 中的模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10846859/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com