代码之家  ›  专栏  ›  技术社区  ›  Tobias Gaertner

codeception测试采用cakephp 3中的“default”数据源而不是“test”

  •  0
  • Tobias Gaertner  · 技术社区  · 7 年前

    我可以通过codeception运行unittests,但是当涉及到数据库和fixture时,它会令人困惑……

    我的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);
        ...
    
    }
    

    这将给出默认数据库中的第一条记录,而不是测试数据库中的第一条记录。我不知道这里缺少什么…很高兴有人帮忙!

    1 回复  |  直到 6 年前
        1
  •  0
  •   Tobias Gaertner    6 年前

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

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

    即使我找不到使用变量数据库测试URL的位置,将其添加到我的.env文件中也解决了这个问题:

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

    变量数据库_test_url在.env中,但对此没有影响…我认为cakephp和codeception并没有神奇地集成得太好。

    不过,我的设备没有加载到UnitTests(类MyTest扩展\codeception\test\unit)中,但问题已解决。

    [更新]夹具加载已修复。我必须手动注入FixtureManager,并在设置函数中加载fxtures。

    推荐文章