doujiao1984 2017-05-29 22:50
浏览 162
已采纳

Laravel测试用例测试重定向到某个路由

I'm writing some test cases for my Laravel app with PHPUnit.

Below is the class that is producing the error:

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

The problem is that when I run PHPUnit I get this error:

PHPUnit 5.7.20 by Sebastian Bergmann and contributors. ....E.. 7 / 7 (100%) Time: 1.65 seconds, Memory: 20.00MB There was 1 error: 1) Test\Feature\UserLoginTest::test_login_user_form_submission Error: Call to undefined method Test\Feature\UserLoginTest::assertRedirectedTo() /Users/shivampaw/Desktop/valet/UltimateCRM/tests/Feature/UserLoginTest.php:37

It says that assertRedirectedTo is an undefined method but I do not have any clue why. I have tried methods like assertRedirect but cannot get this working!

  • 写回答

1条回答 默认 最新

  • duanqi6274 2017-05-29 23:07
    关注

    You need to call assertRedirect and not assertRedirectedTo. Also all assertions need to be called on the response object

    /** @test */
    public function test_login_user_form_submission()
    {
        $this->post('/login', [
            'email'     => $this->admin->email,
            'password'  => 'secret',
        ])->assertRedirect('/'); // <-- Chain it to the response
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?