Codeception: Unit tests with Yii2 and Db modules in one transaction

Created on 20 Oct 2016  路  1Comment  路  Source: Codeception/Codeception

What are you trying to achieve?

I am trying to add records to a database with my models and then check if data exists in a database with DB module with seeInDatabase()

What do you get instead?

If I use cleanup: true in the unit.yml config for Yii2 module, I can't access inserted data using seeInDatabase()

Provide test source code if related

$I = $this->tester;

BlogCategoriesModel::save(['name' => 'new_test', 'is_active' => 1]); // OK
BlogCategoriesModel::save(['name' => 'new_test2', 'is_active' => 1]); // OK
$I->seeInDatabase('blog_categories', ['name' => 'new_test', 'is_active' => 1]); // FAILS
$I->assertEquals(count(BlogCategoriesModel::getAll()), 2); // OK

Details

  • Codeception version: 2.2.5
  • PHP Version: 7.0.11
  • Operating System: Debian
  • Installation type: Composer
  • List of installed packages (composer show)
  • Suite configuration:
# Codeception Test Suite Configuration

# suite for unit (internal) tests.
# RUN `build` COMMAND AFTER ADDING/REMOVING MODULES.

class_name: UnitTester
modules:
  enabled: [Db, Asserts, Yii2]
  config:
         Db:
            dsn: 'pgsql:host=postgres;dbname=database'
            user: 'user'
            password: 'password'
            #dump: 'tests/_output/dump.sql'
            populate: true
            cleanup: true
            reconnect: true
         Yii2:
            cleanup: true
            configFile: 'codeception/config/unit.php'
Yii

Most helpful comment

If I use cleanup: true in the unit.yml config for Yii2 module, I can't access inserted data using seeInDatabase()

yep. That's right.

Probably you don't need Db module at all here, use Yii2 module which works great with Yii activerecord. Just use seeRecord instead of seeInDatabase, in this case you will use the same db connection as your application do and this allows Yii2 cleanup to work.

>All comments

If I use cleanup: true in the unit.yml config for Yii2 module, I can't access inserted data using seeInDatabase()

yep. That's right.

Probably you don't need Db module at all here, use Yii2 module which works great with Yii activerecord. Just use seeRecord instead of seeInDatabase, in this case you will use the same db connection as your application do and this allows Yii2 cleanup to work.

Was this page helpful?
0 / 5 - 0 ratings