拉拉维尔中的PHP单元崩溃并显示消息"A facade has not been set"



我正在尝试编写一些简单的PHPUnit测试,它将测试我的User class和一些DB查询。

我在每个DB查询函数调用上都收到一个A facade has not been set,我不知道为什么。

我是忘记导入或包含一个文件,还是不能用laravelDB类运行PHP单元?

UserTest.php

<?php
namespace TestsUnit;
use AppUser;
use TestsTestCase;
class UserTest extends TestCase
{
const TABLENAME = 'user';
protected static $userid;
public static function setUpBeforeClass(): void
{
$user           = new stdClass();
$user->name     = "Test 1";
$user->email    = "test@test.com";
$user->password = "Password";
self::$userid = User::createNewUser($user);
}

/**
* Create a user
*
* @return void
*/
public function testGetUser(): void
{
$user = new User(self::$userid);
var_dump($user);
if (empty($user)) {
$this->assertTrue(false);
} else {
$this->assertTrue(true);
}
}
}

User.php

<?php
namespace App;
use IlluminateFoundationAuthUser as Authenticatable;
use IlluminateNotificationsNotifiable;
use IlluminateSupportFacadesDB;
use StdClass;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name', 'email', 'password'];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = ['password'];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [];
private $id        = 0;
private $username  = null;
private $email     = null;
private $createdAt = null;
private $updatedAt = null;
const TABLENAME = 'user';
public function __construct(int $userId)
{
$user = DB::table(self::TABLENAME)->where('id', $userId)->get();
$this->id        = $user->id;
$this->username  = $user->name;
$this->email     = $user->email;
$this->createdAt = $user->createdAt;
$this->updatedAt = $user->updatedAt;
}
public function get(string $field): string
{
return $this->{$field};
}
public function updateUser(stdClass $user): boolean
{
$user->id = $this->id;
$result   = DB::table(self::TABLENAME)->update((array) $user);
return ($result >= 1) ? true : false;
}
public static function createNewUser(stdClass $user): int
{
return DB::table(self::TABLENAME)->insertGetId((array) $user);
}
}
<?php 
namespace TestsUnit;
use IlluminateNotificationsNotifiable;
use IlluminateSupportFacadesDB;
use AppUser;
use TestsTestCase;
class UserTest extends TestCase
{
const TABLENAME = 'user';
protected static $userid;

protected function setUp(){
parent::setUp();
}
protected function tearDown()
{
parent::tearDown();
}
public static function setUpBeforeClass(): void
{
$user           = new stdClass();
$user->name     = "Test 1";
$user->email    = "test@test.com";
$user->password = "Password";
self::$userid = User::createNewUser($user);
}

/**
* Create a user
*
* @return void
*/
public function testGetUser(): void
{
$user = new User(self::$userid);
var_dump($user);
if (empty($user)) {
$this->assertTrue(false);
} else {
$this->assertTrue(true);
}
}
}
?>

右侧。这种做法从乞求开始就是错误的。我没有使用正确的模型结构,所以纯DB调用是错误的,因为我们不应该在Laravel项目中使用SQL查询。(除非绝对必要(在生成一个新的模型并根据Laravel文档填充基础后,它开始工作,测试开始通过

https://laravel.com/docs/6.x/eloquent

相关内容

最新更新