场景:
我有以下测试用例,我想测试场景,如果用户发现用户类型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/