Mongodb 如何为yii2项目编写单元测试
我阅读了我找到的所有文档,并设置了codeception来为yii2应用程序编写单元测试 我的项目使用Mongodb 如何为yii2项目编写单元测试,mongodb,unit-testing,yii2,yii2-advanced-app,codeception,Mongodb,Unit Testing,Yii2,Yii2 Advanced App,Codeception,我阅读了我找到的所有文档,并设置了codeception来为yii2应用程序编写单元测试 我的项目使用mongodb作为数据库,当我运行单元测试来测试模型的保存操作时,我发现没有找到db组件 这是真的,因为我使用的是mongodb,而不需要db用于sql。无论如何,当我将设置更改为将mongodb数据库设置重命名为db并且仍然使用mongodb连接设置时,我看到一个错误,这意味着yii2正在尝试使用sqlactiverecord方法 我的测试班: namespace common\tests;
mongodb
作为数据库,当我运行单元测试来测试模型的保存操作时,我发现没有找到db
组件
这是真的,因为我使用的是mongodb
,而不需要db
用于sql。无论如何,当我将设置更改为将mongodb
数据库设置重命名为db
并且仍然使用mongodb连接设置时,我看到一个错误,这意味着yii2正在尝试使用sqlactiverecord方法
我的测试班:
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()
{
}
}
我的common/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')
],
]
);
当我将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.
有人能帮我吗?这是codeception框架模块核心的一个bug。您可以在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',
],
---------
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
---------
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.
'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',
],