升级到laravel 5.8/PHPUnit 8后失败的测试 [英] Failing tests after upgrade to laravel 5.8 / PHPUnit 8

查看:158
本文介绍了升级到laravel 5.8/PHPUnit 8后失败的测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

升级到laravel 5.8/PHPUnit 8后,我有一些测试开始失败.例如以下测试.

After Upgrading to laravel 5.8 / PHPUnit 8 I had several tests that started failing. For example the following test.

public function testAdminCanPromoteUsers()
{
    // While using a admin account try to promote non-admin user
    $this->actingAs($this->admin)
        ->post('user/promote', ['userPromoteId' => $this->user->id, 'name' => $this->user->name]);

    // check if user was promoted to admin
    $user = User::find($this->user->id);
    $this->assertTrue((bool) $user->isAdmin);
}

路线

| POST     | user/allow    | App\Http\Controllers\UserController@allow    | web,admin|
| POST     | user/demote   | App\Http\Controllers\UserController@demote   | web,admin|
| POST     | user/promote  | App\Http\Controllers\UserController@promote  | web,admin|
| POST     | user/reset    | App\Http\Controllers\UserController@reset    | web,admin|
| POST     | user/restrict | App\Http\Controllers\UserController@restrict | web,admin|

用户模型

<?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',
    ];

}

用户迁移

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function(Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email', 191)->unique();
            $table->string('password');
            $table->boolean('isAdmin')->default(false);
            $table->boolean('hasAccess')->default(true);
            $table->rememberToken();
            $table->timestamps();
        });
    }

UserController函数

UserController function

    /**
    * Make an user as admin
    */
    public function promote(Request $request) {
        // Create the collection name
        $thisUsr = User::findOrFail($request->userPromoteId);
        if (strcasecmp($thisUsr->name, $request->name) == 0) {
          $thisUsr->isAdmin = true;
          $thisUsr->save();
          return redirect()->route('userIndex');
        }

        // Error message
        return redirect()->route('userIndex')->withErrors("Failed to Promote User to Admin. User wasn't found.");
    }

PHPUnit结果

1)UserControllerTest :: testAdminCanPromoteUsers
无法断言false为真.

1) UserControllerTest::testAdminCanPromoteUsers
Failed asserting that false is true.

/var/www/tests/Feature/Controllers/UserControllerTest.php:61

/var/www/tests/Feature/Controllers/UserControllerTest.php:61

测试失败,一切仍然在浏览器中进行.关于可能会导致失败的任何想法的任何想法.

Everything still is working in the browser just the test is failing. Any ideas on what may have changed that would cause this to fail.

推荐答案

我已经看到人们建议在函数中添加$this->withoutMiddleware();,但这对我来说效果不佳.尝试在存在令牌的情况下运行它.

I've seen people suggest adding $this->withoutMiddleware(); to the function, but that hasn't worked well for me. Try running it with a token present.

$this->actingAs($this->admin)
        ->post('user/promote', ['userPromoteId' => $this->user->id, 'name' => $this->user->name, '_token' => csrf_token()]);

这篇关于升级到laravel 5.8/PHPUnit 8后失败的测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆