【问题标题】:Having trouble in testing authorization policy in Laravel 5.5在 Laravel 5.5 中测试授权策略时遇到问题
【发布时间】:2019-03-26 09:20:11
【问题描述】:

我在测试授权策略时遇到了问题,它显示了一个有风险的测试,我不知道如何解决这个问题。这是新安装的laravel 5.5

PHPUnit 6.5.13 by Sebastian Bergmann and contributors.

R.                                                                  2 / 2 (100%)

Time: 99 ms, Memory: 16.00MB

There was 1 risky test:

1) Tests\Feature\ExampleTest::testBasicTest
Test code or tested code did not (only) close its own output buffers

OK, but incomplete, skipped, or risky tests!
Tests: 2, Assertions: 2, Risky: 1.

这是我的测试代码:

public function testBasicTest()
{
    $this->get('/home')
        ->assertStatus(403);
}

当我使用dd($this->get('/home')->getContent()); 时,我收到类似这样的错误..

file_get_contents([internal]): failed to open stream: No such file or directory
in Frame.php line 122

这是我的家庭控制器

<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;

class HomeController extends Controller
{
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $this->authorize('create', User::class);
        return view('home');
    }
}

这是我的UserPolicy.php

<?php

namespace App\Policies;

use App\User;
use Illuminate\Auth\Access\HandlesAuthorization;

class UserPolicy
{
    use HandlesAuthorization;

    /**
     * Create a new policy instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    public function create(User $user)
    {
        return true;
    }
}

这是我的AuthServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use App\User;
use App\Policies\UserPolicy;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        User::class => UserPolicy::class,
    ];

    /**
     * Register any authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();

        //
    }
}

补充: 我看到了这个:https://phpunit.readthedocs.io/en/7.4/risky-tests.html 我尝试将所有这些设置为false,但风险仍然存在。

【问题讨论】:

  • 标记phpunit...
  • 你的 home.blade.php 中有什么?
  • @SvenHakvoort,没关系,即使我注释掉return view('home');或者我使用return 'test;,输出还是一样的。
  • @TamilvananN,标记phpunit 是什么意思? phpunit 命令工作正常,如果我创建另一个测试它的工作。我认为问题在于代码$this-&gt;authorize('create', User::class);
  • 如果将--stderr 标志添加到phpunit 会怎样?

标签: php laravel phpunit


【解决方案1】:

设法自己解决我的问题,我刚刚跑了composer update

似乎问题出在包filp/whoops v2.3.0 中,这导致了异常。他们设法在 v2.3.1 中解决了这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-04
    • 2017-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多