I tried creating a unit test for my User service class, however when I run php artisan test
this errors are thrown, I have no idea about this since I’m a newbie in unit testing.
The code works perfectly in controllers, this errors only happens in tests:
This is my UserTest class:
<?php
namespace TestsUnit;
use AppServicesUserService;
use PHPUnitFrameworkTestCase;
class UserTest extends TestCase
{
protected $userService;
protected function setUp(): void
{
parent::setUp();
$this->userService = app(UserService::class);
}
public function test_create_user(): void
{
$userArray = [
'nome' => 'John Doe',
'email' => '[email protected]',
'login' => 'johndoe',
'status' => 1
];
$user = $this->userService->create($userArray);
$this->assertTrue($user->email === $userArray['email']);
}
}
Command executed:
php artisan test
Error received:
FAIL TestsUnitUserTest
⨯ create user 0.05s
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
FAILED TestsUnitUserTest > create user Error
Call to a member function connection() on null
at vendorlaravelframeworksrcIlluminateDatabaseEloquentModel.php:1850
1846▕ * @return IlluminateDatabaseConnection
1847▕ */
1847▕ */
1848▕ public static function resolveConnection($connection = null)
1849▕ {
➜ 1850▕ return static::$resolver->connection($connection);
1851▕ }
New contributor
Fábio Augusto Rodrigues is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.