php - 测试 Laravel 中的重定向功能

标签 php laravel unit-testing phpunit

场景:

我有以下测试用例,我想测试场景,如果用户发现用户类型superadmin,则将用户重定向到/home路由,如果用户未找到重定向他到 /superadmin/setup 路线。尝试了多种方法但无法弄清楚。如何才能通过这个测试?

我使用的是 Laravel 5.4。

测试用例:

<?php

namespace Tests\Feature;

use App\User;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;

class SuperAdminRegistrationTest extends TestCase
{
    use DatabaseMigrations;

    public function testRegistrationForm()
    {
        factory(User::class)->create(['usertype' => 'superadmin']);
        $user = User::getUserFromUsertype('superadmin');
        //If valid user is found with usertype 'superadmin' then
            // $this->get('/home');
            // $this->assertStatus(200);
        //Else user not found with given usertype then
           // $this->get('/superadmin/setup');
           // $this->assertStatus(200);
    }
}

最佳答案

$this->assertRedirectedTo($uri, $with = []);

也许可以尝试类似的事情

此 PHPUnit 方法将断言您是否已重定向到您在参数中提供的 $uri。

https://laravel.com/docs/5.4/testing

您可以在 Laravel 官方测试文档中阅读更多相关内容

关于php - 测试 Laravel 中的重定向功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45675178/

相关文章:

php - Controller 的模型查询函数::如何在 Controller 中调用 w/pagination?

php - Laravel 内置服务器不持久 session 并导致 TokenMismatchException

laravel - 普雷迪斯 : ERR unknown command 'EVAL' at Client. php:370

mysql - Laravel 插入三向数据透视表

ruby-on-rails - 模型和测试中的验证方法之间的错误 :Unit assertion results

java - 即使没有单元测试,构建自动化真的有助于提高生产力吗?

python - 如何在 coverage.py 中使用 .coveragerc 省略导入?

javascript - 像 jquery 一样在 PHP 中使用每个

PHP:返回数组中两个日期之间的所有日期

javascript - 如何在 vue.js 中使用 SVG.js 插件?