D
D
Dmitry Dobryshin2020-11-23 16:49:15
PostgreSQL
Dmitry Dobryshin, 2020-11-23 16:49:15

Why is the record not in the database when the test is run?

I'm writing a test to check a record in a database.

unit.suite.yml

# Codeception Test Suite Configuration

class_name: UnitTester
modules:
    enabled:
      - Asserts
      - Yii2:
            part: [orm, email, fixtures]
      - Db
            populate: true
            cleanup: true
            reconnect: true
    config:
        Db:
            dsn: 'pgsql:host=localhost;dbname=mybase'
            user: 'myuser'
            password: 'correctPassword'

TypesTest.php

<?php
namespace tests\unit\models;
use app\models\Types;

class TypesTest extends \Codeception\Test\Unit
{
    /**
     * @var \UnitTester
     */
    protected $tester;

    protected function _before()
    {
    }

    protected function _after()
    {
    }

    // tests
    public function testEnterType()
    {
        $types = new Types();

        // Пустое значение недопустимо
        $types->name = NULL;
        $this->assertFalse($types->validate(['name']));

        // Больше 100 символов недопустимо
        $types->name = '**** aaaaabbbbbcccccdddddeeeeefffffggggghhhhhiiiiijjjjjkkkkklllllmmmmmnnnnnooooopppppqqqqqrrrrrsssss ****';
        $this->assertFalse($types->validate(['name']));

        $validName = '--TEST TYPE--';
        // Допустимая комбинация
        $types->name = $validName;
        $this->assertTrue($types->validate(['name']));

        // Сохранение данных в базу
        $this->assertTrue($types->validate());
        $this->assertTrue($types->save());
        $this->assertGreaterThan(0, $types->id);
        $this->tester->seeInDatabase('types', ['name' => $validName ]);
    }
}


During the test, a failure occurs on the last line:
Codeception PHP Testing Framework v4.1.6
Powered by PHPUnit 8.5.8 by Sebastian Bergmann and contributors.
Running with seed:

Unit Tests (1) -----------------------------
✖ TypesTest: Enter type (0.03s)
---------------------------------------------

Time: 90 ms, Memory: 12.00 MB
There was 1 failure:

---------
1) TypesTest: Enter type
 Test  tests/unit/models/TypesTest.php:testEnterType
No matching records found for criteria {"name":"--TEST TYPE--"} in table types
Failed asserting that 0 is greater than 0.
#1  Codeception\Module\Db->seeInDatabase
#2  /var/www/basic/tests/_support/_generated/UnitTesterActions.php:1397
#3  /var/www/basic/tests/unit/models/TypesTest.php:48

FAILURES!
Tests: 1, Assertions: 7, Failures: 1.

At the same time, there was no failure in the previous line, and an id greater than 0 was assigned, which means there was a record in the database, but when you try to check for the presence of a record, it reports that there is no such record.

What did I set up wrong and how can I fix it so that the last check succeeds?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavlvdv, 2021-03-17
@DimkaI

In Yii2 config add transaction: false ( https://codeception.com/for/yii )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question