Laravel / PHP单元测试在更新后失败

时间:2019-11-13 17:03:39

标签: php laravel phpunit

在将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();
    }

故障似乎从这里开始,我在调用此例程,该例程动态创建一个新表。管理员是在上面的设置中定义的。

    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 Question Failing tests after upgrade to laravel 5.8 / PHPUnit 8

任何建议将不胜感激。

1 个答案:

答案 0 :(得分:2)

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

// 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');
}