首頁  >  文章  >  後端開發  >  Yii2 codecept unit 類別找不到

Yii2 codecept unit 類別找不到

WBOY
WBOY原創
2016-09-08 08:43:541108瀏覽

我的測試程式碼如下

<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>

執行的時候會抱以下錯誤:

<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>

下程式碼為我參考官方的

<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>

我的問題到底出在哪裡?謝謝各位不吝賜教。

回覆內容:

我的測試程式碼如下

<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>

執行的時候會抱以下錯誤:

<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>

下程式碼為我參考官方的

<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>

我的問題到底出在哪裡?謝謝各位不吝賜教。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn