Testing Api using PHPUnit Laravel

First copy the .env.example file and rename it to .env.testing:

Comment 2020-04-16 001122

Comment 2020-04-16 001140

Then, execute:

touch test.sqlite

Comment 2020-04-16 001411

A test.sqlite file will be generated in the D: / laragon / www / laraveauth directory:

Comment 2020-04-16 001514

Then we modify the database configuration of the .env.testing file to:

DB_CONNECTION=sqlite
DB_DATABASE="D:/laragon/www/laraveauth/test.sqlite"

Comment 2020-04-16 001636

Next execute:

php artisan make: test TaskApiTest

Comment 2020-04-16 001758

The file is located at:

Comment 2020-04-16 001848

Next, before modifying this file, execute

php artisan route:list

Take a look at the related routes requested by api task

Comment 2020-04-16 002115

Let's implement the method of creating a test first:

The method is named relatively easy to understand;

Comment 2020-04-16 002422

Send a post request to / api / task; the final code is as follows:

public function testCanCreateTask()
{
    $formData = [
        'title' => 'sample test task title',
        'description' => 'sample test task description',
        'due' => 'next friday',
    ];

    $response = $this->post(route('tasks.store'), $formData);

    $response->assertStatus(201);
}

Since phpunit exists in the vendor \ bin folder:

Comment 2020-04-16 003303

So: the console executes:

.\vendor\bin\phpunit.bat

Comment 2020-04-16 003410

result:

Comment 2020-04-16 003435

Error resolution:

1. APP_KEY is not set in the env.testing file:

Comment 2020-04-16 003554

Just copy one of the .env files;

Execute again:

.\vendor\bin\phpunit.bat

result:

Comment 2020-04-16 003725

Without clues, we need this:

$this->withoutExceptionHandling();

Comment 2020-04-16 004149

Execute again:

.\vendor\bin\phpunit.bat

You can find more error trace information:

Comment 2020-04-16 004307

The result is that we did not log in before executing the store method:

The revised and execution results are as follows:

Comment 2020-04-16 005019

Correction method: laragon opens pdo_sqlite extension. [As long as PHP opens the sqlite extension]

Comment 2020-04-16 005633

Let's add a trait in the class

use RefreshDatabase;

This trait will refresh the database every time the unit is tested. [Execute migration once for migration]

Execute again:

.\vendor\bin\phpunit.bat

Comment 2020-04-16 005752

The test passed!

Next we assert Json

Comment 2020-04-16 010020

The result failed:

Comment 2020-04-16 010112

Modify it and the result still fails:

Comment 2020-04-16 010242

Modify again:

Comment 2020-04-16 010414

The test passed!

The rest of the tests are not written.

The documentation is here: Test Guide

The TaskApiTest.php file is as follows:

<?php

namespace Tests\Feature;

use App\Task;
use App\User;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class TaskApiTest extends TestCase
{
    use RefreshDatabase;

    protected $user;

    public function setUp(): void
    {
        parent::setUp();
        $this->user = factory(User::class)->create();
        $this->actingAs($this->user, 'api');
    }

    /**
     * A basic feature test example.
     *
     * @return void
     */
    public function testCanCreateTask()
    {
        $due = Carbon::parse('next friday');
        $formData = [
            'title' => 'sample test task title',
            'description' => 'sample test task description',
            'due' => $due,
        ];
        $this->withoutExceptionHandling();

        $response = $this->post(route('tasks.store'), $formData);
        $response->assertStatus(201)
            ->assertJson(['data' => $formData]);
    }

    public function testCanShowTask()
    {
        $task = factory(Task::class)->create();
        $this->user->tasks()->save($task);
        $response = $this->get(route('tasks.show', $task->id));
        $response->assertStatus(200);
    }

    public function testUpdateTask()
    {
        $task = factory(Task::class)->create();
        $this->user->tasks()->save($task);

        $due = Carbon::parse('next friday');

        $formData = [
            'title' => 'sample test task title',
            'description' => 'sample test task description',
            'due' => $due,
        ];
        $this->withoutExceptionHandling();

        $response = $this->put(route('tasks.update', $task->id), $formData);
        $response->assertStatus(200)
            ->assertJson(['data' => $formData]);
    }

    public function testDelete()
    {
        $task = factory(Task::class)->create();
        $this->user->tasks()->save($task);
        $response = $this->delete(route('tasks.destroy', $task));
        $response->assertStatus(200)->assertJson(['message' => 'Success deleted!']);
    }

    public function testShowAllTasks()
    {
        $tasks = factory(Task::class, 3)->create();
        $this->user->tasks()->saveMany($tasks);
        $response = $this->get(route('tasks.index'));
        $response->assertStatus(200)->assertJson($tasks->toArray());
    }
}

Guess you like

Origin www.cnblogs.com/dzkjz/p/12709840.html