yii2单元测试未在db中添加记录

时间:2018-10-14 15:34:43

标签: php yii2 codeception phpunit-testing

请帮助我了解单元测试如何在Yii2中工作。我写了测试:

<?php
namespace frontend\tests\unit\models;

use Yii;
use app\models\Category;

class ContactFormTest extends \Codeception\Test\Unit
{
    public function testAddCorrect()
    {
        $model = new Category();

        $model->attributes = [
            'name' => 'Tester',
            'cpu' => 'new-tester',
            'description' => 'very important letter subject',
            'image' => 'body of current message',
        ];
        $this->assertTrue($model->save());



        $this->assertTrue($model->name === 'Tester');
        $id = $model->id;

        $newmodel = Category::find()->where(['id' => $id])->one(); 
        $this->assertTrue($newmodel->name === 'Tester');

    }

我期望它会在数据库中添加新记录。但事实并非如此。测试工作正常,但是注意到db中增加了我不明白为什么?而且我必须编写测试模型+数据库的测试?

unit.suit.yml

suite_namespace: frontend\tests\unit
actor: UnitTester
modules:
    enabled:
        - Yii2:
            part: [orm, email, fixtures]
        - Asserts
        - Db:
            dsn: 'mysql:host=docker.for.mac.localhost;dbname=store-light'
            user: 'ysk_dbu'
            password: 'ysk_pass'
            cleanup: false
            #            cleanup: true

0 个答案:

没有答案