[SOLVED] Does unit test work with scenarios?


(Sasha Tesh) #1

Does unit test work with scenarious?

I keep on getting error:




Test  tests/unit/models/ModeNameTest.php:testSuccess

                                                                  

  [yii\base\ErrorException] Trying to get property of non-object 



This is the code for testSuccess method in ModeNameTest model:




public function testSuccess()

    {

        $model = new UserNamesTestModel(['scenario' => UserNamesTestModel::SCENARIO_CLIENT]);


        $model->user_id = 1;

        $model->name = 'Test Name';




        expect_that($model->save());


    }



Error it happening on this line:




expect_that($model->save());



UserNamesTestModel extends ActiveRecord, thefore such method as save(), update() should be there and I have no idea why it is throwing such Exception…

Did anybody cross this problem before?

P.s. it is not related to rules validation, because $model->errors return empty array.


(Sasha Tesh) #2

Okay, I found the problem, but do not know how to solve another issue now:

I have method defined as validation rules in my model and it has Yii::$app->user->identity->id, which takes authorized user id to check whatever about him.

This is why it throws Exception.

But how can I make test in this case if I am making a simple unit test?