我阅读了找到的所有文档,并设置了codeception来编写yii2应用程序的单元测试。
我的项目使用mongodb
作为数据库,当我运行单元测试来测试我的模型的保存操作时,我发现没有找到db
组件。
这是真的,因为我使用的是mongodb
,sql不需要db
。无论如何,当我更改设置,将mongodb
数据库设置重命名为db
,并且仍然使用mongodb连接设置时,我看到错误,这意味着yii2正在尝试使用SQL activerecord方法。
我的测试课程:
namespace commontests;
use commonmodelsDeveloper;
use commontestsfixturesDeveloperFixture;
use FakerFactory;
class DeveloperTest extends CodeceptionTestUnit
{
/**
* @var commontestsUnitTester
*/
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 FakerGenerator 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()
{
}
}
My commont/config/test-local.php
<?php
return yiihelpersArrayHelper::merge(
require __DIR__ . '/main.php',
require __DIR__ . '/main-local.php',
require __DIR__ . '/test.php',
[
'components' => [
'mongodb' => require_once ('conf.d/test-db.php')
],
]
);
My common/config/conf.d/test-db.php
<?php
return
[
'class' => 'yiimongodbConnection',
'dsn' => 'mongodb://mongodb/mytestdb', //Using docker container
];
我的固定设备类别:
<?php
namespace commontestsfixtures;
use yiimongodbActiveFixture;
/**
* Class Developer
* Active fixture for using Developer model.
*
* @package commontestsfixtures
*/
class DeveloperFixture extends ActiveFixture
{
public $modelClass = commonmodelsDeveloper::class;
}
在那之后,我运行vendor/bin/codecept -c core/common run unit models/DeveloperTest
,我看到下面的错误:
---------
1) DeveloperTest: Saving
Test tests/unit/models/DeveloperTest.php:testSaving
[yiibaseInvalidConfigException] 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 yiibaseBaseObject->__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 yiibaseBaseObject->__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
[yiibaseUnknownMethodException] Calling unknown method: yiimongodbCommand::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.
有人能帮我吗?
这是用于代码感知的框架模块核心中的一个错误。您可以在common/config/test-local.hp:中复制数据库连接
'db' => [
'class' => yiimongodbConnection::class,
'dsn' => 'mongodb://localhost:27017/app_test_db',
],
'mongodb' => [
'class' => yiimongodbConnection::class,
'dsn' => 'mongodb://localhost:27017/app_test_db',
],