PHPUnit Laravel 哈希不可用

标签 php laravel phpunit laravel-facade

我在 Laravel 中有一个单元测试,用于测试看起来像这样的 API 调用,但是在运行它时出现以下运行时错误:

RuntimeException: A facade root has not been set.

我在 setup 方法中创建一个用户,目的是在 tearDown() 方法中再次删除它,然后运行我的身份验证测试。

首先,有没有更好的方法来做我想做的事?例如在不接触数据库的情况下模拟用户?其次,如何设置“外观根”或该错误的确切含义是什么?为了创建一个 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'),
  ]);
}

最佳答案

尝试改用它:

\Hash::make('test@test.com'),

最好使用 bcrypt() 全局助手而不是 Hash::make()

此外,将此添加到 setUp() 方法:

parent::setUp();

关于PHPUnit Laravel 哈希不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41396107/

相关文章:

php - 模拟测试对象的公共(public)方法

php - 将外部 pdf 文档加载到 div 中

php - 在电子邮件中设置回复字段

php - PHP 和 Python 中的冒泡排序

使用 PHP7.2 和 7.1 运行的 phpunit 测试比使用 PHP7.0 运行时慢 3 倍

php - Laravel CRUD Controller 测试

c# - 如何使用 POST 方法向公共(public)网站发送请求以及如何查找请求的参数(如来源和目的地等)

php - 在 Lumen 5.2 中配置日志轮换

laravel - 在 Controller 中使用 foreach 时出现语法错误

laravel - 等待本地主机...永远不会结束