我最近开始在现有的Yii2应用程序中实现代码测试。
测试结构已大大改变,但在创建新项目并使用现有应用程序中的默认文件后,我可以运行测试。不幸的是,每个测试错误如下:
6) HomeCest: Check open
Test tests/functional/HomeCest.php:checkOpen
[yii\base\InvalidConfigException] The "basePath" property must be set.
Scenario Steps:
1. $I->amOnPage("/") at tests/functional/HomeCest.php:11
#1 /Users/rrbot/Sites/MYSITE/vendor/yiisoft/yii2/base/View.php:222
#2 /Users/rrbot/Sites/MYSITE/vendor/yiisoft/yii2/base/View.php:150
#3 /Users/rrbot/Sites/MYSITE/vendor/yiisoft/yii2/base/Controller.php:378
#4 /Users/rrbot/Sites/MYSITE/frontend/controllers/SiteController.php:114
#5 frontend\controllers\SiteController->actionIndex
#6 /Users/rrbot/Sites/MYSITE/vendor/yiisoft/yii2/base/InlineAction.php:55
#7 /Users/rrbot/Sites/MYSITE/vendor/yiisoft/yii2/base/Controller.php:154
#8 /Users/rrbot/Sites/MYSITE/vendor/yiisoft/yii2/base/Module.php:454
#9 /Users/rrbot/Sites/MYSITE/vendor/yiisoft/yii2/web/Application.php:100
#10 /Users/rrbot/Sites/MYSITE/vendor/symfony/browser-kit/Client.php:315
我的frontend / config / test-local.php看起来如此:
<?php
// config/test-local.php
$config = yii\helpers\ArrayHelper::merge(
require(__DIR__ . '/main.php'),
require(__DIR__ . '/main-local.php'),
[
'id' => 'app-tests',
'basePath' => dirname(__DIR__),
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=127.0.0.1;dbname=mydb',
'username' => 'someser',
'password' => 'somepass',
'charset' => 'utf8',
],
]
]
);
return $config;
正如你所看到的,我正在声明basePath,实际上也是在main.php中重新声明它。