簡體   English   中英

Laravel CRUD控制器測試

[英]Laravel CRUD controller test

基本上我必須為許多Laravel控制器編寫測試,其中大多數是CRUD(讀取,存儲,更新),並且大多數邏輯都放在那些內部(繼承代碼,而不是我的)。

我需要做的是從用戶的角度自動化測試。 因此,我需要點擊所有端點並針對真實數據庫進行測試,並檢查一切是否正常。

我幾乎沒有測試經驗,但從我收集的控制器應該通過集成/驗收測試進行測試。 現在我通過擴展Laravel的TestCase測試Read方法做得很好,這是一個例子:

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的存儲方法,使用創建的令牌測試其他控制器並在測試完成后刪除,所以它可以再次執行。

我無法概念化所有這些,因為我還沒有真正做過多少測試。

這是使用令牌和用戶數據進行測試的示例 -

<?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('abc@gmail.com', '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());

}

}

希望這會幫助你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM