Php 如何使用Laravel测试授权重定向?

Php 如何使用Laravel测试授权重定向?,php,laravel,unit-testing,laravel-5,laravel-5.1,Php,Laravel,Unit Testing,Laravel 5,Laravel 5.1,我已经手动测试了我想要的场景: 管理员用户可以访问站点的/code部分。 普通用户被重定向(302)回/dashboard,当他们转到/qr时,会收到一条消息很抱歉不允许您进入 手动测试通过,但laravel测试失败 我使用的是laravel 5.1 对管理员用户的测试: public function testAdminViewCodes() { //create an admin user $user = factory(App\User::class

我已经手动测试了我想要的场景:

管理员用户可以访问站点的
/code
部分。 普通用户被重定向(302)回
/dashboard
,当他们转到
/qr
时,会收到一条消息
很抱歉不允许您进入

手动测试通过,但laravel测试失败

我使用的是
laravel 5.1

对管理员用户的测试:

public function testAdminViewCodes()
    {
        //create an admin user
        $user = factory(App\User::class, 'admin')->create();

        $this->actingAs($user)
            ->visit('/codes')
            ->seePageIs('/codes')
            ->see('Codes');
    }
    public function testNormalViewCodesFail()
    {
        //create a normal user
        $normal_user = factory(App\User::class)->create();

        //TODO: Fix this failing test FFS

        $this->actingAs($normal_user)
             ->visit('/qr')
             ->seePageIs('/dashboard')
             ->see('Sorry you are not allowed there');
}
正常用户测试:

public function testAdminViewCodes()
    {
        //create an admin user
        $user = factory(App\User::class, 'admin')->create();

        $this->actingAs($user)
            ->visit('/codes')
            ->seePageIs('/codes')
            ->see('Codes');
    }
    public function testNormalViewCodesFail()
    {
        //create a normal user
        $normal_user = factory(App\User::class)->create();

        //TODO: Fix this failing test FFS

        $this->actingAs($normal_user)
             ->visit('/qr')
             ->seePageIs('/dashboard')
             ->see('Sorry you are not allowed there');
}
测试结果

There was 1 failure:

1) AdminTest::testNormalViewQRCodesFail
Did not land on expected page [http://localhost/dashboard].

Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'http://localhost/dashboard'
+'http://localhost/codes'
我认为工厂可能有问题,似乎总是在创建管理员用户:

$factory->define(App\User::class, function (Faker\Generator $faker) {
    return [
        'email' => $faker->email,
        'password' => bcrypt(str_random(10)),
        'remember_token' => str_random(10),
        'is_admin' => false,
    ];
});

$factory->defineAs(App\User::class, 'admin', function ($faker) use ($factory) {
    $user = $factory->raw(App\User::class);

    return array_merge($user, ['is_admin' => true]);
});
我很抱歉这个问题持续了这么久,但还有一个相关的问题。我正在使用
中间件
测试用户是否为管理员:

<?php

namespace RMS\Http\Middleware;

use Closure;

class IsAdminMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (app()->env === 'testing') {
            return $next($request);
        }

        if (! $request->user()->isAdmin()) {
          return redirect()->route('dashboard')
              ->with('message', 'Sorry you are not allowed there');
        }

        return $next($request);
    }
}
并适用于以下路线:

Route::group(['middleware' => ['auth', 'isadmin']], function()
{
    Route::resource('user', 'UserController');
});

中间件被忽略了吗?我肯定不会在没有中间件的情况下添加
用法语句。

我建议使用Authenticate用户实例
Auth::login($User)有关更多详细信息,请阅读

此方法适用于Laravel 5.x及以上版本

您有两个选项:

  • 更好地为用户工厂创建测试,就像它可以生成您想要的用户类型一样
  • 在用户生成后中断调试器以手动检查它
我建议您创建测试,因为您现在有疑问,而且将来可能会意外地破坏出厂代码,因为它并不那么明显。

作为旁白:这些测试并不意味着是
用户体验
测试。为此,将进行
验收
功能
测试。其中一个比较流行的工具是。它与浏览器会话相结合或可以模拟浏览器会话,并获得完整的用户体验渲染

每个文档可在以下文档中找到:

验收测试:“验收测试可以从用户的角度涵盖标准但复杂的场景。通过验收测试,您可以确信,遵循所有定义的场景的用户不会出现错误。”

功能测试:“功能测试您模拟web请求($\u GET和$\u POST变量),并将其发送到您的应用程序中,该应用程序返回HTML响应。”


单元测试:“在将代码片段耦合在一起之前对其进行测试也非常重要。通过这种方式,您可以确保某些深层次隐藏的特性仍然有效,即使功能测试或验收测试没有涵盖它。这也证明您生成了稳定且可测试的代码。

在用户生成后使用调试器手动检查它。
如何执行此操作。python有一个很酷的工具叫做
pdb()
ipdb()
是否有一个与laravel等价的工具?PHP的调试器被调用。在某些IDE中有集成的支持,例如,是的,但是这不允许你在解释的中间进行检查……或者是这样吗?它也允许你在运行时改变变量并评估自定义代码。当你习惯了它,你可以比使用var_dump或类似工具更快地调试时间。我看到它在与IDE集成时工作,你的IDE偏好是什么?啊,那就是
laravel 5.2
我提到过我在
laravel 5.1
。升级似乎有点太多了。这种方法对5.1也是有效的,好吧,但我已经在这样做了,我想用
actingAs
helper方法让我深入研究一下,然后再告诉你这不是一个单元测试,但是在拉威尔的帮助下进行功能测试。@Pheagey请在你的答案中写下不同类型测试的定义,然后链接到可靠的资源。维基百科并没有做到这一点。如果问题是
单元
功能
集成
端到端
测试,我会尽快更改问题。这是一个答案,还是旁白/评论?因为你没有回答问题,所以不得不将你作为已接受的答案删除。我正在测试授权,我没有提到它是一个单元测试。