S
S
Sergey Beloventsev2020-08-21 22:40:23
Laravel
Sergey Beloventsev, 2020-08-21 22:40:23

How to fix a bug in a unit test?

The User class has this method:

public static function register(RegisterRequest $request):self
{
         return static::create([
             'name'=>$request->name,
             'email'=>$request->email,
             'password'=>Hash::make($request->password),
             'verify_token' => Str::random(),
             'status' => self::STATUS_WAIT,
         ]);
    }

This is how I try to test it:
public function create():void
    {
        $user=User::register($this->request);
        self::assertNotEmpty($user);
        self::assertEquals($this->name,$user->name);
        self::assertEquals($this->email,$user->email);
        self::assertNotEmpty($user->password);
        self::assertEquals($this->password,$user->password);
        self::assertTrue($user->isWait());
        self::assertFalse($user->isActive());
    }

This is how I create a RegisterRequest:
public function setUp(): void
    {
        parent::setUp();
        $this->request= $this->createMock(RegisterRequest::class);
        $this->request->name=$this->name;
        $this->request->email=$this->email;
        $this->request->password=$this->password;
    }

I am getting this error:
Caused by
ReflectionException: Class config does not exist

/www/app/vendor/laravel/framework/src/Illuminate/Container/Container.php:809
/www/app/vendor/laravel/framework/src/Illuminate/Container/Container.php:691
/www/app/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:796
/www/app/vendor/laravel/framework/src/Illuminate/Container/Container.php:637
/www/app/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:781
/www/app/vendor/laravel/framework/src/Illuminate/Container/Container.php:1284
/www/app/vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php:288
/www/app/vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php:101
/www/app/vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php:77
/www/app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1342
/www/app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1308
/www/app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1114
/www/app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1031
/www/app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1067
/www/app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1020
/www/app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1734
/www/app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1746
/www/app/app/Entity/User.php:84
/www/app/tests/Unit/RegisterTest.php:69

Refers to this line:
'status' => self::STATUS_WAIT,

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Wells, 2020-08-22
@Sergalas

These are not really unit tests. If you want to test unity - make mocks, drop statics and eloquent.
And the way you test is possible only with the initialization of the framework, i.e. it is necessary to extend from TestCase'a that lies in tests.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question