升级到laravel 5.8 / PHPUnit 8后失败的测试

问题描述 投票:0回答:1

升级到laravel 5.8 / PHPUnit 8之后,我进行了几次测试,但都开始失败。例如以下测试。

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函数

    /**
    * 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为真。

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

只是测试失败,浏览器中的所有内容仍在运行。关于可能会导致失败的任何更改的想法。

php laravel phpunit
1个回答
0
投票

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

$this->actingAs($this->admin)
        ->post('user/promote', ['userPromoteId' => $this->user->id, 'name' => $this->user->name, '_token' => csrf_token()]);
© www.soinside.com 2019 - 2024. All rights reserved.