Laravel 5.4登录测试用例提供错误的状态代码。

时间:2017-05-03 11:55:57

标签: php laravel-5.4

我正在尝试在laravel 5.4上的Login Page上编写测试用例 以下是我的代码。

测试/单元/ LoginTest.php

<?php

namespace Tests\Unit;

use Tests\TestCase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class LoginTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testURL()
    {
        $response = $this->call('GET','/login');
        $this->assertEquals(200,$response->status());
    }

    public function testBlankFields(){
        $response =  $this->get('/login');
        $response->assertStatus(200);
        $response->assertSee('User Login');
    }

    public function testWrongValues(){
        $response =  $this->post('/login',['email'=>'Brijeshdubey@gmail.com',
                                                  'password'=>'Brijesh']);
        $response->assertStatus(401);
        $response->assertRedirect('/login');


    }
}

参见下图我收到了错误的状态代码302而不是401

enter image description here

  

谁能告诉我我缺少的地方?

     
    

如果有人知道在laravel 5.4编写测试用例的好方法那么     请让我知道我很困惑。

  

谢谢

1 个答案:

答案 0 :(得分:0)

响应代码302对应于找到的&#39; (http://http.cat/302),这是Laravel中常用的响应代码,用于指定重定向。所以,会发生什么事情,你被重定向。

如果您愿意,可以将此返回JSONResponse,只需要进行一些小的更改。