測試

測試

測試程式產生器

php artisan make:test UserControllerTest

php artisan make:test UserTest --unit
<?php
namespace Tests\Feature;

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

class ArticleController extends TestCase
{
    public function index()
    {
        $this->assertTrue(true);
    }
}

執行測試

./vendor/bin/phpunit
./vendor/bin/phpunit tests/Feature/ExampleTest

HTTP 測試

$response = $this->json('POST', '/user', ['name' => 'Sally']);
<?php
namespace Tests\Feature;

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

class UserController extends TestCase
{
    public function index()
    {
        $response = $this->get('/api/user');
        $response->assertStatus(200);
    }
}

assertJson

<?php

class ExampleTest extends TestCase
{
    public function testBasicExample()
    {
        // $response = $this->get('/api/user/1');
        $response = $this->json('GET', '/api/user/1');
        $response
            ->assertStatus(200)
            ->assertJson([
                'user_name'=>'alincode',
                'email'=>'alincode@gmail.com',
            ]);
    }
}

assertExactJson

<?php

class ExampleTest extends TestCase
{
    public function testBasicExample()
    {
        // $response = $this->get('/api/user/1');
        $response = $this->json('GET', '/api/user/1');
        $response
            ->assertStatus(200)
            ->assertExactJson([
                'id'=>1,
                'user_name'=>'alincode',
                'full_name'=>'AILIN LIOU',
                'email'=>'alincode@gmail.com',
                'created_at'=>null,
                'updated_at'=>null
            ]);
    }
}

Assert

$response->assertStatus($code);
$response->assertRedirect($uri);
$response->assertHeader($headerName, $value = null);
$response->assertCookie($cookieName, $value = null);
$response->assertPlainCookie($cookieName, $value = null);
$response->assertSessionHas($key, $value = null);
$response->assertSessionHasErrors(array $keys);
$response->assertSessionMissing($key);
$response->assertJson(array $data);
$response->assertJsonFragment(array $data);
$response->assertJsonMissing(array $data);
$response->assertExactJson(array $data);
$response->assertJsonStructure(array $structure);
$response->assertViewIs($value);
$response->assertViewHas($key, $value = null);

JSON API 測試

參考資料

results for ""

    No results matching ""