【问题标题】:Codeception Tests take 'default' datasource instead of 'test' in CakePHP 3在 CakePHP 3 中 Codeception 测试采用“默认”数据源而不是“测试”
【发布时间】:2018-11-22 14:22:22
【问题描述】:

我可以通过 Codeception 运行 UnitTests,但是当涉及到数据库和固定装置时,它会令人困惑......

我的 codeception.dist.yml

namespace: App\TestSuite\Codeception
paths:
    tests: tests
    output: tmp/tests
    data: tests/Fixture
    support: src/TestSuite/Codeception
    envs: tests/Envs
settings:
    bootstrap: bootstrap.php
    colors: true
    memory_limit: 1024M
actor_suffix: Tester
extensions:
    enabled:
        - Codeception\Extension\RunFailed
modules:
    config:
        Db:
            dsn: 'mysql:host=%DB_HOST_TEST%;dbname=%DB_NAME_TEST%'
            user: '%DB_USER_TEST%'
            password: '%DB_PASSWORD_TEST%'
            dump: tests/_data/dump.sql
            cleanup: true # reload dump between tests
            populate: true # load dump before all tests
            reconnect: true
    enabled:
        - Db
params:
    - env

dump.sql 在运行我的测试时被导入到测试数据库中。未插入夹具...

然后在我的测试中我做这样的事情(缩短):

<?php
namespace App\Test\Unit\Model\Behavior;

use App\Model\Behavior\HashableBehavior;
use App\Model\Entity\User;
use Cake\ORM\TableRegistry;
use Cake\Datasource\ConnectionManager;
use Cake\TestSuite\TestCase;

/**
 * App\Model\Behavior\HashableBehavior Test Case
 */
class HashableBehaviorTest extends \Codeception\Test\Unit
{
/**
 * Test subject
 *
 * @var \App\Model\Behavior\HashableBehavior
 */
public $Hashable;

public $fixtures = [
    'app.Users',
    'app.mandators'
];

/**
 * Test subject
 *
 * @var \App\Model\Table\UsersTable
 */
public $UsersTable;

/**
 * setUp method
 *
 * @return void
 */
public function setUp()
{
    parent::setUp();
    $config = TableRegistry::getTableLocator()->exists('Users') ? [] : ['className' => UsersTable::class];
    $this->UsersTable = TableRegistry::getTableLocator()->get('Users', $config);

    $this->Hashable = new HashableBehavior($this->UsersTable);
}

/**
 * tearDown method
 *
 * @return void
 */
public function tearDown()
{
    unset($this->Hashable);

    parent::tearDown();
}

/**
 * Test beforeSave method
 *
 * @return void
 * @throws \Exception
 */
public function testBeforeSave()
{
    $user = $this->UsersTable->get(1);
    ...

}

这给了我默认数据库中的第一条记录,而不是测试数据库中的第一条记录。我不知道这里缺少什么......很高兴得到任何帮助!

【问题讨论】:

  • 听起来像 CakePHPs 夹具管理器没有运行(这是默认连接被交换为测试连接的地方)。您在使用 github.com/cakephp/codeception 吗?如果没有,您可能想尝试一下,因为 CakePHP 本身并没有与 Codeception 集成。
  • 是的,我使用 github.com/cakephp/codeception - 主要问题是周文档...
  • 您的tests/Unit.suite.yml 文件是什么样的?具体来说,它是否启用了\Cake\Codeception\Framework 模块?

标签: php unit-testing cakephp-3.0 codeception


【解决方案1】:

再次检查所有代码后,我在 tests/bootstrap.php 中找到了这行:

if (getenv('db_dsn')) {
    putenv('DATABASE_URL=' . getenv('db_dsn'));
    putenv('DATABASE_TEST_URL=' . getenv('db_dsn'));
}

即使我找不到使用变量 DATABASE_TEST_URL 的地方,将其添加到我的 .env 文件中也解决了它:

export db_dsn="mysql://db_usr:db_password@db_host/db_name?encoding=utf8&timezone=UTC&cacheMetadata=true&quoteIdentifiers=false&persistent=false"

变量 DATABASE_TEST_URL 位于 .env 中,但对此没有影响……我认为 cakePHP 和 Codeception 并没有神奇地集成得太好。

我的装置仍然没有加载到 UnitTests(MyTest 类扩展 \Codeception\Test\Unit)中 - 但问题已解决。

[更新] 固定装置加载已修复。我必须手动注入 FixtureManager 并在 setUp 函数中加载我的 fxtures。

【讨论】:

    猜你喜欢
    • 2021-09-10
    • 1970-01-01
    • 2012-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-01
    • 1970-01-01
    • 2015-11-26
    相关资源
    最近更新 更多