【发布时间】:2011-08-21 22:12:27
【问题描述】:
我想在zend项目中测试一个模型,
<?php
//require_once('CustomModelBase.php');
class Application_Model_User extends Custom_Model_Base {
protected function __construct() {
parent::__construct();
}
static function create(array $data) {
}
static function load($id) {
}
static function find($name, $order=null, $limit=null, $offset=null) {
);
}
}
application/model 文件夹下的模型,它扩展了一个基类 Custom_Model_Base,它与类 User 在同一文件夹下。
在我的测试中,我尝试以这种方式创建一个新的 User 对象
<?php
class Model_UserTest extends ControllerTestCase
{
protected $user2;
public function setUp() {
parent::setUp();
$this->user2 = new Application_Model_User2();
}
public function testCanDoTest() {
$this->assertTrue(true);
}
}
这是 CustomModelBase.php: 抽象类 Custom_Model_Base { 受保护的函数 __construct($adapter=null) {} }
它给了我错误,说“PHP 致命错误:在第 4 行的 \application\models\User.php 中找不到类 'Custom_Model_Base'”,我在 User.php 中包含“CustomModelBase.php”,它给了我另一个错误“PHP 致命错误:从第 13 行的 D:\PHP\apache2\htdocs\ID24_Xiao\tests\application\models\UserTest.php 中的上下文 'Model_User2Test' 调用受保护的 Application_Model_User::__construct()”
那我该怎么办呢?谁能给点建议?
【问题讨论】:
-
您的构造函数是否有理由受到保护?
-
不是您问题的解决方案,但应该是 $this->user2 = new Application_Model_User();而不是 $this->user2 = new Application_Model_User2();
标签: unit-testing zend-framework phpunit