Samat  Zhanbekov
Samat Zhanbekov

Reputation: 413

Models unit testing on Yii2

I'm trying to build Yii2 app through unit testing and i have some questions about it.

class UsersTest extends \Codeception\TestCase\Test
{
   /**
    * @var \UnitTester
    */
    protected $users;

    protected function _before()
    {
        $this->users = new \app\models\Users;
    }

    protected function _after()
    {
    }

    // tests
    public function testGeId()
    {

    }

}

When i try to run this test class i have fatal error message that Users class not found. What cause of the problem and how to solve it?

There is readme file in Yii2 tests folder which tell us to setup yii2-faker and yii2_basic_tests database. What are these two things and why i should to use them?

Thank you.

Upvotes: 2

Views: 5925

Answers (3)

Abdallah Elsabeeh
Abdallah Elsabeeh

Reputation: 616

defined the auto loader in the phpunit xml configuration file

<?xml version="1.0" encoding="utf-8" ?>
<phpunit bootstrap="./vendor/autoload.php">

    <testsuites>
        <testsuite name="The project's test suite">
            <directory>./tests</directory>
        </testsuite>
    </testsuites>

</phpunit>

Upvotes: 0

Samat  Zhanbekov
Samat Zhanbekov

Reputation: 413

It was need to create application instance in tests/_bootstrap.php. It must be following code in that file:

require('/../vendor/autoload.php');
require('/../vendor/yiisoft/yii2/Yii.php');

$config = require('config/web.php');

(new yii\web\Application($config));

Upvotes: 4

Sepuka
Sepuka

Reputation: 54

Possibly you

settings:
    bootstrap: _bootstrap.php

in codeception.yml is wrong? This file include vendor/autoload.php and class names resolved

Upvotes: 1

Related Questions