2017-05-03 123 views
0

我正試圖在laravel上登錄頁面上編寫測試用例5.4 下面是我的代碼。Laravel 5.4登錄測試用例給出了錯誤的狀態碼。

測試/單位/ 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'=>'[email protected]', 
                'password'=>'Brijesh']); 
     $response->assertStatus(401); 
     $response->assertRedirect('/login'); 


    } 
} 

見下面的圖片我收到錯誤的狀態代碼302,而不是401

enter image description here

誰能告訴我在哪裏我缺乏?

,如果有人知道寫測試用例laravel 5.4好辦法再 請讓我知道我很迷惑。

謝謝

+0

您可以使用控制器方法更新問題嗎? –

+0

我正在使用內置Auth的laravel 5.4 –

回答

0

響應碼302對應於「找到」(http://http.cat/302),這是在Laravel常用的響應代碼時重定向給出了。所以,發生什麼是你被重定向。

如果您願意,您可以將此退貨設置爲JSONResponse,只需要稍作更改即可。

+0

即使同樣的狀態我得到 –

+0

@BrijeshDubey如上所述,它的代碼發出重定向 –

+0

你能幫我把我寫在testWrongValues()這個方法檢查電子郵件和密碼如果無效,則重定向到具有狀態的相同頁面。 –