Squ*_*gs. 4 php phpunit laravel laravel-facade
我在Laravel中进行了单元测试,用于测试看起来像这样的API调用,但是在运行时遇到以下运行时错误:
RuntimeException: A facade root has not been set.
Run Code Online (Sandbox Code Playgroud)
我在setup方法中创建了一个用户,打算在tearDown()方法中再次删除它,然后运行我的auth测试.
首先,有没有更好的方法来做我想要的?例如,在不触及数据库的情况下模拟用户?其次,如何设置"立面根"或该错误究竟意味着什么?为了创建一个Dummy用户,我已经尝试不打扰散列该特定字段,但是错误似乎转移到模型,其中(再次)使用了Hash外观类.
是否有任何其他步骤来设置环境,以便这些外观可用于测试?
提前致谢.
use Illuminate\Support\Facades\Hash;
/*
* Make sure the structure of the API call is sound.
*/
public function testAuthenticateFailed()
{
$this->json('POST', $this->endpoint,
[ 'email' => 'test@test.com',
'password' => 'password',
])
->seeJsonStructure([
'token'
]);
}
//create a user if they don't already exist.
public function setup()
{
$user = User::create([
'company_id' => 9999,
'name'=>'testUser',
'email' => 'test@test.com',
'password' => 'password',
'hashed_email' => Hash:make('test@test.com'),
]);
}
Run Code Online (Sandbox Code Playgroud)
尝试使用此代替:
\Hash::make('test@test.com'),
Run Code Online (Sandbox Code Playgroud)
使用bcrypt()全局帮助器而不是使用全局帮助器是个好主意Hash::make()
另外,将此添加到setUp()方法:
parent::setUp();
Run Code Online (Sandbox Code Playgroud)