Home  >  Article  >  Backend Development  >  Yii2 codecept unit class cannot be found

Yii2 codecept unit class cannot be found

WBOY
WBOYOriginal
2016-09-08 08:43:541069browse

My test code is as follows

<code><?php

use app\models\UserSource;

class UserSourceUintTest extends \PHPUnit_Framework_TestCase
{
    protected function setUp()
    {
    }

    protected function tearDown()
    {
    }

    // tests
    public function testMe()
    {

        $params = [
            'user_id' => 1,
            'channel_id' => 2,
        ];
        $this->assertTrue(UserSource::recordUserReg($params));



    }
}</code>

The following error will occur when executing:

<code>Unit Tests (1) --------------------------------------------------------------------------
E UserSourceUintTest: Me 
-----------------------------------------------------------------------------------------


Time: 40 ms, Memory: 6.00MB

There was 1 error:

---------
1) UserSourceUintTest: Me
 Test  tests/unit/UserSourceUintTest.php:testMe
                                                   
  [Error] Class 'app\models\UserSource' not found  
                                                   
#1  UserSourceUintTest->testMe

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
</code>

The code below is for me to refer to the official one

<code>Classical Unit Testing
Unit tests in Codeception are written in absolutely the same way as it is done in PHPUnit:

<?php
class UserTest extends \Codeception\Test\Unit
{
    public function testValidation()
    {
        $user = User::create();

        $user->username = null;
        $this->assertFalse($user->validate(['username']));

        $user->username = 'toolooooongnaaaaaaameeee';
        $this->assertFalse($user->validate(['username']));

        $user->username = 'davert';
        $this->assertTrue($user->validate(['username']));           
    }
}</code>

What exactly is my problem? Thank you for your generous advice.

Reply content:

My test code is as follows

<code><?php

use app\models\UserSource;

class UserSourceUintTest extends \PHPUnit_Framework_TestCase
{
    protected function setUp()
    {
    }

    protected function tearDown()
    {
    }

    // tests
    public function testMe()
    {

        $params = [
            'user_id' => 1,
            'channel_id' => 2,
        ];
        $this->assertTrue(UserSource::recordUserReg($params));



    }
}</code>

The following error will occur when executing:

<code>Unit Tests (1) --------------------------------------------------------------------------
E UserSourceUintTest: Me 
-----------------------------------------------------------------------------------------


Time: 40 ms, Memory: 6.00MB

There was 1 error:

---------
1) UserSourceUintTest: Me
 Test  tests/unit/UserSourceUintTest.php:testMe
                                                   
  [Error] Class 'app\models\UserSource' not found  
                                                   
#1  UserSourceUintTest->testMe

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
</code>

The code below is for me to refer to the official one

<code>Classical Unit Testing
Unit tests in Codeception are written in absolutely the same way as it is done in PHPUnit:

<?php
class UserTest extends \Codeception\Test\Unit
{
    public function testValidation()
    {
        $user = User::create();

        $user->username = null;
        $this->assertFalse($user->validate(['username']));

        $user->username = 'toolooooongnaaaaaaameeee';
        $this->assertFalse($user->validate(['username']));

        $user->username = 'davert';
        $this->assertTrue($user->validate(['username']));           
    }
}</code>

What exactly is my problem? Thank you for your generous advice.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn