2017-07-12 13 views
0

私は単体テストでかなり新しく、ログインページをテストしようとしています 私の目標はこのユニットです: - > 「/」 - >ではない場合 - >私はTestLoginTrueでテストする場合、「/ログイン」ルートにLaravel phpunit + multiprocessを使用したログイン資格証明

<?php 

namespace Tests\Feature; 

use App\Domain\Core\Models\User; 
use Illuminate\Support\Facades\Hash; 
use Illuminate\Support\Facades\Session; 
use Tests\TestCase; 
use Illuminate\Foundation\Testing\WithoutMiddleware; 
use Illuminate\Foundation\Testing\DatabaseMigrations; 
use Illuminate\Foundation\Testing\DatabaseTransactions; 

class userTest extends TestCase 
{ 
    use DatabaseMigrations; 
    /** 
    * A basic test example. 
    * 
    * @return void 
    */ 
    public function testLoginTrue() 
    { 
     $credential = [ 
      'email' => '[email protected]', 
      'password' => 'user' 
     ]; 
     $this->post('login',$credential)->assertRedirect('/'); 
    } 

    public function testLoginFalse() 
    { 
     $credential = [ 
      'email' => '[email protected]', 
      'password' => 'usera' 
     ]; 
     $this->post('login',$credential)->assertRedirect('/login'); 
    } 
} 

をリダイレクトして成功した「/」に戻るが、私はTestLoginFalseをしようとすると...それはのように同じ返しますTestLoginTrue、それは '/ login'ルートにとどまるべきです アイデア?

プラス私は、私はすでに私の最初のアイデアがあるので、私は、ログインページにアクセスすることができませんでしたログインしたときかどうかを確認してみたい: パブリック関数testLoginTrue()

{ 
    $credential = [ 
     'email' => '[email protected]', 
     'password' => 'user' 
    ]; 
    $this->post('login',$credential) 
     ->assertRedirect('/') 
     ->get('/login') 
     ->assertRedirect('/'); 
} 

けど...それは

を返します。

1)Tests \ Feature \ userTest :: testLoginTrue BadMethodCallException: メソッド[get]がリダイレクトに存在しません。

どのように正しく行うには?事前

答えて

0

おかげで私はLaravel 5.4テストのフォローで立ち往生ビットはケースをリダイレクトもいます。

回避策として、$response->assertSessionHasErrors()をチェックしてください。それが動作するはずです。この方法:

また
public function testLoginFalse() 
{ 
    $credential = [ 
     'email' => '[email protected]', 
     'password' => 'incorrectpass' 
    ]; 

    $response = $this->post('login',$credential); 

    $response->assertSessionHasErrors(); 
} 

は、testLoginTrue()にあなたがチェックして、そのセッションにエラーが不足している:これは役立ちます

$response = $this->post('login',$credential); 
$response->assertSessionMissing('errors'); 

希望を!

+0

回避策:)ありがとう、それは完全に動作しています。 。 。リダイレクトされたものについてはちょっと変わっています... testLoginFalseはエラーを返しますが、それに応じてコントローラにリダイレクトされません。とにかくあなたの回避策に固執します.D –

関連する問題