【问题标题】:Laravel / PHP Unit Test failing after update更新后 Laravel / PHP 单元测试失败
【发布时间】:2020-03-09 13:07:15
【问题描述】:

在将 Laravel 从 5.4 更新到 5.8 和 PHPUnit 8 之后,一些测试开始失败。它似乎与 Authenticatable 有关,但我不确定如何修复它。在阅读了其他一些帖子之后,我似乎需要更改用户模型。运行我的一项测试时,我看到以下错误

1) DataViewControllerTest::testImportWithRecords
TypeError: Argument 1 passed to Laravel\BrowserKitTesting\TestCase::actingAs() must implement interface Illuminate\Contracts\Auth\Authenticatable, null given, called in /var/www/tests/Feature/Controllers/DataViewControllerTest.php on line 41

用户模型

<?php

namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Auth;

class User extends Authenticatable
{
    use Notifiable;
    public static $snakeAttributes = false;

    /**
     * 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', 'remember_token',
    ];

}

由于整个 DataViewControllerTest.php 很长,我附上了相关部分。

    public function setUp() {
           parent::setUp();
           Artisan::call('migrate');
           Artisan::call('db:seed');
           Session::setDefaultDriver('array');
           $this->manager = app('session');

           // find admin and test user accounts
           $this->admin = User::where('name', '=', 'admin')->first();
           $this->user = User::where('name', '=', 'test')->first();
    }

失败似乎从这里开始我正在调用这个动态创建新表的例程。 Admin 在上面的设置中定义。

    public function createTestTable($tblname, $path, $file) {
            // create a test collection
            $this->collection = (new TestHelper)->createCollection('collection1');

            $this->actingAs($this->admin)
                 ->visit('table/create')
                 ->type($tblname, 'imprtTblNme')
                 ->type('1', 'colID')
                 ->attach($path . $file, 'fltFile')
                 ->press('Import')
                 ->assertResponseStatus(200)
                 ->see('Edit Schema')
                 ->submitForm('Submit', [ 'col-0-data' => 'string', 'col-0-size' => 'default',
                                         'col-1-data' => 'string', 'col-1-size' => 'default',
                                         'col-2-data' => 'string', 'col-2-size' => 'default',
                                         'col-3-data' => 'string', 'col-3-size' => 'default',
                                         'col-4-data' => 'string', 'col-4-size' => 'default',
                                         'col-5-data' => 'string', 'col-5-size' => 'default',
                                         'col-6-data' => 'string', 'col-6-size' => 'default',
                                         'col-7-data' => 'string', 'col-7-size' => 'default',
                                         'col-8-data' => 'string', 'col-8-size' => 'default',
                                         'col-9-data' => 'string', 'col-9-size' => 'default',
                                         'col-10-data' => 'string', 'col-10-size' => 'default',
                                         'col-11-data' => 'string', 'col-11-size' => 'default',
                                         'col-12-data' => 'string', 'col-12-size' => 'default',
                                         'col-13-data' => 'string', 'col-13-size' => 'default',
                                         'col-14-data' => 'string', 'col-14-size' => 'default',
                                         'col-15-data' => 'string', 'col-15-size' => 'default',
                                         'col-16-data' => 'string', 'col-16-size' => 'default',
                                         'col-17-data' => 'string', 'col-17-size' => 'default',
                                         'col-18-data' => 'string', 'col-18-size' => 'default',
                                         'col-19-data' => 'string', 'col-19-size' => 'default',
                                         'col-20-data' => 'string', 'col-20-size' => 'big',
                                         'col-21-data' => 'text', 'col-21-size' => 'default',
                                         'col-22-data' => 'text', 'col-22-size' => 'default',
                                         'col-23-data' => 'string', 'col-23-size' => 'default',
                                         'col-24-data' => 'string', 'col-24-size' => 'default',
                                         'col-25-data' => 'string', 'col-25-size' => 'default',
                                         'col-26-data' => 'string', 'col-26-size' => 'default',
                                         'col-27-data' => 'string', 'col-27-size' => 'default',
                                         'col-28-data' => 'string', 'col-28-size' => 'big',
                                         'col-29-data' => 'text', 'col-29-size' => 'default',
                                         'col-30-data' => 'text', 'col-30-size' => 'default',
                                         'col-31-data' => 'string', 'col-31-size' => 'default' ])
                 ->assertResponseStatus(200)
                 ->see('Load Data')
                 ->press('Load Data')
                 ->see('Table(s)')
                 ->assertResponseStatus(200);
    }

我前几天发布的相关问题。 Related QuestionFailing tests after upgrade to laravel 5.8 / PHPUnit 8

任何建议将不胜感激。

【问题讨论】:

  • User 模型看起来不错。看起来您发送给actingAs 的任何内容都是空的。您是否偶然在控制器构造函数中使用了闭包中间件?
  • 能否将 DataViewControllerTest.php 内容添加到问题中?
  • @Peterverleg 我更新了我的问题。
  • @adam 在 DataViewController 构造函数中它们是 $this->middleware('auth');
  • 如何为测试创建管理员用户?运行时似乎不存在

标签: php laravel phpunit


【解决方案1】:

以下代码可能没有返回任何内容:

// find admin and test user accounts
$this->admin = User::where('name', '=', 'admin')->first();

这可能与您的播种机或迁移有关。

我建议以下步骤: 1. 检查你的播种器,看看是否添加了管理员用户。

考虑添加此代码:

$this->admin = User::where('name', '=', 'admin')->first();
if($this->admin == null){
  $this->fail('No admin user present');
}

【讨论】:

  • @AD7six 谢谢你的提示,我已经修改了我的遮阳篷
  • 重构了我的测试并将您的建议添加到设置中。不知何故不完全确定,但测试再次通过感谢您的帮助。
猜你喜欢
  • 2022-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-27
  • 2015-02-05
  • 1970-01-01
  • 2016-06-28
  • 2015-01-12
相关资源
最近更新 更多