【问题标题】:How write unit test for yii2 project如何为yii2项目编写单元测试
【发布时间】:2018-11-09 09:59:34
【问题描述】:

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

我的项目使用mongodb 作为数据库,当我运行单元测试以测试模型的保存操作时,我看到db 组件未找到。

这是真的,因为我使用 mongodb 并且不需要 db 用于 sql。无论如何,当我更改我的设置以将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.

谁能帮帮我?

【问题讨论】:

    标签: mongodb unit-testing yii2 yii2-advanced-app codeception


    【解决方案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',
        ],
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-15
      • 1970-01-01
      • 2019-03-23
      • 2018-01-17
      • 2012-01-06
      • 2019-03-17
      • 2016-04-02
      相关资源
      最近更新 更多