2016-08-23 19 views
4

基本的には、ほとんどがCRUD(読み込み、保存、更新)である多くのLaravelコントローラのテストを記述する必要があり、ほとんどのロジックはそれらの内部に配置されています(Inherited code、not mine)。Laravel CRUDコントローラテスト

私がする必要があることは、ユーザーの観点からテストを自動化することです。だから私はすべてのエンドポイントをヒットし、実際のデータベースに対してテストし、すべてがうまくいくかどうかを確認する必要があります。

私はテストでほとんど経験はありませんが、コントローラを集めたものから、統合/受け入れテストでテストする必要があります。今、私はテストと罰金はLaravelのTestCaseクラスを拡張してメソッドを読みました、ここでは一例です:

class SongsTest extends TestCase 
{ 
    public function testBasicIndex() 
    { 
     $arguments = []; 

     $response = $this->call('GET', '/songs', $arguments); 

     $this->assertResponseOk(); 
     $this->seeJson(); 
    } 

    /** 
     * @dataProvider providerSearchQuery 
    */ 
    public function testSearchIndex($query) 
    { 
     $arguments = ['srquery' => $query]; 

     $response = $this->call('GET', '/songs', $arguments); 

     $this->assertResponseOk(); 
     $this->seeJson(); 
    } 

    public function providerSearchQuery() 
    { 
     return array(
      array('a'), 
      array('as%+='), 
      array('test?Someqsdag8hj$%$') 
      ); 
    } 


    public function testGetSongsById() 
    { 
     $id = 1; 

     $response = $this->call('GET', '/songs/' . $id); 

     $this->assertContains($response->getStatusCode(), [200, 404]); 
     $this->seeJson(); 

     if($response->getStatusCode() == 404) 
     { 
      $content = json_decode($response->getContent()); 
      $this->assertContains($content->message[0], ['Song not found', 'Item not active']); 
     } 
    } 
} 

これらのテストは、エンドポイントをヒットし、応答が200で、フォーマットはJSONといくつか他のものであるかどうかを確認します。これらはうまく動作します。

のは、例えば、我々はUserControllerで、およびユーザーを作成するメソッドを持っているとしましょう:私はに問題がある持っている何

。その後、TokensControllerでこのユーザーを使用して、トークンを保護された要求で将来記憶する必要があるトークンを作成する必要があります。

私の質問:

私は自動化するにはどうすればよい:(モックなし)の試験データベースで実際のユーザーを作成してUserControllerでのstoreメソッドのテスト、と他のコントローラをテストし、そのユーザーの電子メールを使用してTokensControllerのstoreメソッドのテスト作成されたトークンを削除し、テストが完了したら削除して、再度実行することができます。

私は本当に多くのテストをしていないので、すべてを概念化することはできません。

答えて

0

これはテスト用のトークンと、ユーザーのデータを使用する例である -

<?php 

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

class PostTest extends TestCase 
{ 
    use WithoutMiddleware; 
    public $token = "lrSFMf0DpqRAh4BXTXWHp5VgFTq4CqA68qY3jG2CqvcpNTT6m0y9Qs6OdpSn"; 

/* 
    A browser that receives a 302 response code HAS to redirect which means it will take the URL in the response and submit a new request. The result you see in your browser is the redirected page. 

    Unit testing does not redirect. Your unit test is only doing what you direct it to do. If your unit test should test for the redirect then you evaluate the response and the correct assertion is 302 and not 200. 
*/ 
public function testExample() 
{ 
    $this->assertTrue(true); 
} 

public function testLogin() 
{ 
    $this->visit('/') 
    ->type('[email protected]', 'email') 
    ->type('123456', 'password') 
    ->press('Login') // type submit - value/button - lable 
    ->seePageIs('/Wall'); // for redirect url 
} 


public function testFavourite() 
{ 
    $this->testLogin(); 
    $request = [ 
     'user_id' => '172', 
     'token' => $this->token, 
     'post_id' => '500' 
    ]; 

    $response = $this->call('POST', '/DoFavoriteDisc',$request); 
    $this->assertEquals(200, $response->getStatusCode()); 

} 

} 

が、これはあなたを助けることを願っています。

関連する問題