sofiDev img

Irnovi

Laravel:Apa Saja Yang Perlu ditest - Part 2

Dalam pengembangan aplikasi dengan pendekatan TDD, ada beberapa aspek penting yang perlu diuji untuk memastikan bahwa aplikasi Anda berfungsi dengan baik dan sesuai dengan spesifikasi. Berikut adalah beberapa hal utama yang perlu diuji dalam aplikasi Laravel:

1. Model

2. Controller

3. Routes

4. Middleware

5. Views

6. API

7. Form Validation

8. Database

9. Authentication & Authorization

10. Business Logic

11. Performance

12. User Interface (UI)

Contoh Tes dengan Pest

Berikut adalah contoh beberapa jenis tes menggunakan Pest di Laravel:

Model Test

// tests/Unit/ArticleTest.php

use App\Models\Article;

it('creates an article with valid attributes', function () {
    $article = Article::create([
        'title' => 'Test Article',
        'content' => 'Test Content',
    ]);

    expect($article)->toBeInstanceOf(Article::class);
    expect($article->title)->toBe('Test Article');
});

Controller Test

// tests/Feature/ArticleControllerTest.php

use App\Models\Article;

it('stores a new article', function () {
    $response = $this->post('/articles', [
        'title' => 'Test Article',
        'content' => 'Test Content'
    ]);

    $response->assertStatus(201);
    $this->assertDatabaseHas('articles', [
        'title' => 'Test Article',
        'content' => 'Test Content'
    ]);
});

Route Test

// tests/Feature/RoutesTest.php

it('allows access to the articles index route', function () {
    $response = $this->get('/articles');

    $response->assertStatus(200);
});

Kesimpulan

Menguji berbagai aspek dari aplikasi Anda sangat penting untuk memastikan stabilitas, keamanan, dan kinerja yang baik. Dengan menggunakan Pest dan pendekatan TDD, Anda dapat memastikan bahwa aplikasi Laravel Anda berfungsi dengan benar dan siap untuk digunakan oleh pengguna.