2016-08-23 148 views
4

基本上我必須爲許多Laravel控制器編寫測試,其中大多數是CRUD(讀取,存儲,更新),並且大部分邏輯放置在那些(繼承代碼,不是我的)中。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中的存儲方法的測試中,通過使用用戶的電子郵件,測試其他控制器,TokensController的存儲方法的測試創建的令牌和刪除,一旦測試完成,所以它可以再次執行。

我只是不能概念化所有,因爲我沒有真正做過很多測試。

回答

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()); 

} 

} 

希望這會幫助你。