dongnuo6310
2017-08-14 13:04
浏览 47
已采纳

在Laravel中测试重定向功能

Scenario :

I have following test case in which I want to test scenario as, if user found with usertype superadmin then redirect user to /home route and if user not found redirect him to /superadmin/setup route. Tried multiple approch but can't figure out. How can pass this test?

I'm using Laravel 5.4.

Test Case :

<?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);
    }
}

图片转代码服务由CSDN问答提供 功能建议

场景:

我有以下测试用例,我想在其中测试 如果用户使用usertype superadmin 找到用户,则将用户重定向到 / home 路由,如果找不到用户,则将其重定向到 / superadmin / setup 路由 。 尝试了多个approch但无法弄清楚。 怎么能通过这个测试?

我正在使用Laravel 5.4。

测试用例:
 &lt;?php  
 
namespace Tests \ Feature; 
 
use App \ User; 
use Tests \ TestCase; 
use Illuminate \ Foundation \ Testing \ DatabaseMigrations; 
 
class SuperAdminRegistrationTest扩展TestCase 
 {
使用DatabaseMigrations; 
 \  n public function testRegistrationForm()
 {
 factory(User :: class) - &gt; create(['usertype'=&gt;'superadmin']); 
 $ user = User :: getUserFromUsertype('superadmin')  ; 
 //如果找到有效用户的用户类型'superadmin',则
 // $ this-&gt; get('/ home'); 
 // $ this-&gt; assertStatus(200); 
 /  /找不到给定用户类型的用户,然后
 // $ this-&gt; get('/ superadmin / setup'); 
 // $ this-&gt; assertStatus(200); 
} 
} 
    
 

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • doucheng1884 2017-08-14 16:00
    最佳回答
    $this->assertRedirectedTo($uri, $with = []);
    

    maybe try something like that

    This PHPUnit method will assert whether you have been redirected to the $uri you provide within the arguments.

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

    you can read more about this in the official laravel documentation for testing

    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题