1
votes

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.

1
What is wrong in my code? - Евгений Майстренко
Did you read documentation: yiiframework.com/doc-2.0/…. "An attribute will be validated if and only if it is an active attribute declared in scenarios() and is associated with one or multiple active rules declared in rules()." - TomaszKane
"The default implementation of scenarios() will return all scenarios found in the validation rule declaration method yii\base\Model::rules()". - Евгений Майстренко
I do the same code like yours why I am getting the error like class not found can you show me your unit.suite.yml - Anway Kulkarni

1 Answers

1
votes

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