Class 'CDbTestCase' 使用 yii 在 phpunit 中找不到错误

Class 'CDbTestCase' not found error in phpunit with yii

我正在尝试使用 yii 进行单元测试。我转到 protected\tests\unit 文件夹和 运行 phpunit\AccountTest

我收到错误 Class 'CDbTestCase' not found in ......tests\unit\AccountTest.php

我是单元测试的新手,不确定是否设置正确。谁能告诉我我做错了什么?

如果您的 bootstrap.php 脚本中的路径不正确,就会发生这种情况。

您的 protected/tests/bootstrap.php 文件应如下所示:

<?php

// change the following paths if necessary
$yiit=dirname(__FILE__).'/../../../framework/yiit.php';
$config=dirname(__FILE__).'/../config/test.php';

require_once($yiit);
require_once(dirname(__FILE__).'/WebTestCase.php');

Yii::createWebApplication($config);

注意第 4-5 行。路径将根据您安装 Yii 和您的应用程序的方式而有所不同。查看文件的位置,并确保这些路径指向正确的位置。

我还建议从 protected/tests 内部 运行ning PHPUnit,而不是 protected/tests/unit。例如:

cd protected\tests
phpunit unit\AccountTest.php

如果 运行 来自 unit 文件夹,PHPUnit 有时无法 bootstrap 正确。