Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 如何为yii2项目编写单元测试_Mongodb_Unit Testing_Yii2_Yii2 Advanced App_Codeception - Fatal编程技术网

Mongodb 如何为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;

我阅读了我找到的所有文档,并设置了codeception来为yii2应用程序编写单元测试

我的项目使用
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',
    ],