我阅读找到的所有文档并设置代码接收,以编写yii2应用程序的单元测试。
我的项目使用mongodb
作为数据库,并且当我运行单元测试以测试模型的保存操作时,我看到找不到db
组件。
是真的,因为我使用的是mongodb
,而对于SQL则不需要db
。无论如何,当我将设置更改为将mongodb
数据库设置重命名为db
并仍然使用mongodb连接设置时,我看到错误,这意味着yii2试图使用SQL activerecord方法。
我的测试班:
namespace common\tests;
use common\models\Developer;
use common\tests\fixtures\DeveloperFixture;
use Faker\Factory;
class DeveloperTest extends \Codeception\Test\Unit
{
/**
* @var \common\tests\UnitTester
*/
protected $tester;
/**
* @return array
*/
public function _fixtures()
{
return [
'user' => [
'class' => DeveloperFixture::class,
'dataFile' => codecept_data_dir() . 'developer.php'
]
];
}
/**
* Test to saving user in database.
* We are using Factory object to create dynamic test cases.
*/
public function testSaving()
{
// use the factory to create a Faker\Generator instance
$faker = Factory::create();
$developer = new Developer([
'name' => $faker->name,
'description' => $faker->sentences
]);
$this->assertTrue($developer->save(), 'Developer object saved into database.');
}
protected function _before()
{
}
protected function _after()
{
}
}
我的commont / config / test-local.php
<?php
return yii\helpers\ArrayHelper::merge(
require __DIR__ . '/main.php',
require __DIR__ . '/main-local.php',
require __DIR__ . '/test.php',
[
'components' => [
'mongodb' => require_once ('conf.d/test-db.php')
],
]
);
我的common / config / conf.d / test-db.php
<?php
return
[
'class' => '\yii\mongodb\Connection',
'dsn' => 'mongodb://mongodb/mytestdb', //Using docker container
];
我的设备类:
<?php
namespace common\tests\fixtures;
use yii\mongodb\ActiveFixture;
/**
* Class Developer
* Active fixture for using Developer model.
*
* @package common\tests\fixtures
*/
class DeveloperFixture extends ActiveFixture
{
public $modelClass = \common\models\Developer::class;
}
此后,我运行vendor/bin/codecept -c core/common run unit models/DeveloperTest
,我看到以下错误:
---------
1) DeveloperTest: Saving
Test tests/unit/models/DeveloperTest.php:testSaving
[yii\base\InvalidConfigException] Failed to instantiate component or class "db".
#1 /app/vendor/yiisoft/yii2/di/Instance.php:139
#2 /app/vendor/yiisoft/yii2/di/Container.php:428
#3 /app/vendor/yiisoft/yii2/di/Container.php:364
#4 /app/vendor/yiisoft/yii2/di/Container.php:156
#5 /app/vendor/yiisoft/yii2/di/Instance.php:167
#6 /app/vendor/yiisoft/yii2/di/Instance.php:137
#7 /app/vendor/yiisoft/yii2/test/DbFixture.php:41
#8 /app/vendor/yiisoft/yii2/base/BaseObject.php:109
#9 yii\base\BaseObject->__construct
#10 /app/vendor/yiisoft/yii2/di/Container.php:375
#1 /app/vendor/yiisoft/yii2/di/Container.php:428
#2 /app/vendor/yiisoft/yii2/di/Container.php:364
#3 /app/vendor/yiisoft/yii2/di/Container.php:156
#4 /app/vendor/yiisoft/yii2/di/Instance.php:167
#5 /app/vendor/yiisoft/yii2/di/Instance.php:137
#6 /app/vendor/yiisoft/yii2/test/DbFixture.php:41
#7 /app/vendor/yiisoft/yii2/base/BaseObject.php:109
#8 yii\base\BaseObject->__construct
#9 /app/vendor/yiisoft/yii2/di/Container.php:375
#10 /app/vendor/yiisoft/yii2/di/Container.php:156
--
There was 1 failure:
---------
1) DeveloperTest: Saving
Test tests/unit/models/DeveloperTest.php:testSaving
Developer object saved into database.
Failed asserting that false is true.
#1 /app/core/common/tests/unit/models/DeveloperTest.php:42
当我将test-local.php中的mongodb
更改为db
时,我看到以下错误日志:
---------
1) DeveloperTest: Saving
Test tests/unit/models/DeveloperTest.php:testSaving
[yii\base\UnknownMethodException] Calling unknown method: yii\mongodb\Command::checkIntegrity()
#1 /app/vendor/yiisoft/yii2/base/BaseObject.php:222
#2 /app/vendor/yiisoft/yii2/test/InitDbFixture.php:96
#3 /app/vendor/yiisoft/yii2/test/InitDbFixture.php:78
#4 /app/vendor/yiisoft/yii2/test/FixtureTrait.php:117
#5 /app/vendor/symfony/event-dispatcher/EventDispatcher.php:212
#6 /app/vendor/symfony/event-dispatcher/EventDispatcher.php:44
--
There was 1 failure:
---------
1) DeveloperTest: Saving
Test tests/unit/models/DeveloperTest.php:testSaving
Developer object saved into database.
Failed asserting that false is true.
#1 /app/core/common/tests/unit/models/DeveloperTest.php:42
ERRORS!
Tests: 1, Assertions: 1, Errors: 1, Failures: 1.
有人可以帮助我吗?
答案 0 :(得分:1)
这是用于代码接收的框架模块核心中的错误。您可以在common / config / test-local.php中复制数据库连接:
'db' => [
'class' => yii\mongodb\Connection::class,
'dsn' => 'mongodb://localhost:27017/app_test_db',
],
'mongodb' => [
'class' => yii\mongodb\Connection::class,
'dsn' => 'mongodb://localhost:27017/app_test_db',
],