Php Laravel测试用例测试重定向到特定路径

Php Laravel测试用例测试重定向到特定路径,php,laravel,unit-testing,laravel-5,phpunit,Php,Laravel,Unit Testing,Laravel 5,Phpunit,我正在用PHPUnit为我的Laravel应用程序编写一些测试用例 下面是产生错误的类: <?php namespace Test\Feature; use Tests\TestCase; use App\Models\User; use Illuminate\Foundation\Testing\DatabaseMigrations; class UserLoginTest extends TestCase { use DatabaseMigrations; pu

我正在用PHPUnit为我的Laravel应用程序编写一些测试用例

下面是产生错误的类:

<?php

namespace Test\Feature;

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

class UserLoginTest extends TestCase
{
    use DatabaseMigrations;

    public function setUp()
    {
        parent::setUp();
        $this->admin = factory(User::class)->create([
                'is_admin'  => true,
                'password'  => bcrypt('secret'),
            ]);
    }

    /** @test */
    public function test_login_user_form()
    {
        $this->get('/login')
            ->assertStatus(200);
    }

    /** @test */
    public function test_login_user_form_submission()
    {
        $this->post('/login', [
                'email'     => $this->admin->email,
                'password'  => 'secret',
            ]);

        $this->assertRedirectedTo('/');
    }
}

您需要调用
assertRedirect
,而不是
assertRedirectedTo
。此外,还需要对
响应
对象调用所有断言

/** @test */
public function test_login_user_form_submission()
{
    $this->post('/login', [
        'email'     => $this->admin->email,
        'password'  => 'secret',
    ])->assertRedirect('/'); // <-- Chain it to the response
}
/**@测试*/
公共功能测试\登录\用户\表单\提交()
{
$this->post(“/login”[
'email'=>this->admin->email,
“密码”=>“密码”,

])->assertRedirect(“/”);//如果我想assertRedirectRoute怎么办?@Shiv没有这样的方法,但是你可以使用
route()
helper函数,比如:
assertRedirect(route('some_route'))