Yii2 ActiveRecord Unit-testing with codeception.

I'm trying to use yii2-codeception. I have a class named Client that extends ActiveRecord.

rules() function:

public function rules()
{
    return [
        [['user_agent', 'ip_address', 'language', 'city_slug'], 'required'],
        [['created_at', 'updated_at'], 'integer'],
        [['user_agent'], 'string', 'max' => 255],
        [['ip_address'], 'string', 'max' => 12],
        [['language'], 'string', 'max' => 16],
        [['city_slug'], 'string', 'max' => 32]
    ];
}

I make a file named ClientTest in tests\codeception\unit\models folder that contents:

<?php

namespace tests\codeception\unit\models;

use Yii;
use yii\codeception\TestCase;
use Codeception\Specify;
use genesisc\clientForm\models\Client;
use Codeception\Util\Debug;

class ClientTest extends TestCase
{
    use Specify;

    public function testCreateWithoutAdditionalFields()
    {
        $this->specify("model must automaticaly create non-meta fields",      function() {
            $model = new Client;
            $x = $model->validate();
            Debug::debug($model->errors);die();
            $this->assertTrue($model->save(), 'Client saved without additional info');
        });

    }

}

And when I run test using 'codecept run unit --debug' command I see that $model->errors is empty array, but Client model has required fields.

Upvotes: 1

Views: 1757

Answers (1)

Ok. That was my bad. In the Client model were a function beforeValidate() that returns null value.

Upvotes: 1

Related Questions