Testing Api using PHPUnit Laravel

首先复制.env.example文件重命名为.env.testing:

批注 2020-04-16 001122

批注 2020-04-16 001140

然后,执行:

touch test.sqlite

批注 2020-04-16 001411

将在 D:/laragon/www/laraveauth 目录下生成一个test.sqlite文件:

批注 2020-04-16 001514

然后我们将.env.testing文件数据库配置修改为:

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

批注 2020-04-16 001636

接下来执行:

php artisan make:test TaskApiTest

批注 2020-04-16 001758

文件位于:

批注 2020-04-16 001848

接下来,修改这个文件之前,执行一下

php artisan route:list

查看一下api task请求的相关route:

批注 2020-04-16 002115

先来实现测试创建Task的方法:

方法命名为比较容易理解的就行;

批注 2020-04-16 002422

发送post请求到/api/task;最后代码如下:

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

由于phpunit存在于 vendor\bin文件夹下:

批注 2020-04-16 003303

所以:控制台执行:

.\vendor\bin\phpunit.bat

批注 2020-04-16 003410

结果:

批注 2020-04-16 003435

错误解决:

1.env.testing文件中APP_KEY未设置:

批注 2020-04-16 003554

拷贝一个.env文件中的即可;

再次执行:

.\vendor\bin\phpunit.bat

结果:

批注 2020-04-16 003725

没有头绪,我们需要这个:

$this->withoutExceptionHandling();

批注 2020-04-16 004149

再次执行:

.\vendor\bin\phpunit.bat

可以发现出现了更多的错误trace信息:

批注 2020-04-16 004307

结果就是 我们执行store方法前未登录:

修改后及执行结果如下:

批注 2020-04-16 005019

修正方法:laragon开启pdo_sqlite扩展。【只要php开启sqlite的扩展就可以了】

批注 2020-04-16 005633

我们再在类内添加一个trait

use RefreshDatabase;

这个trait会在每次单元测试的时候刷新数据库。【对migration执行一次migrate】

再次执行:

.\vendor\bin\phpunit.bat

批注 2020-04-16 005752

测试通过!

接下来我们断言Json

批注 2020-04-16 010020

结果失败:

批注 2020-04-16 010112

修改一下及结果仍然失败:

批注 2020-04-16 010242

再次修改:

批注 2020-04-16 010414

测试通过!

其余的测试不写了。

文档看这里:测试指南

TaskApiTest.php文件如下:

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

posted @ 2020-04-16 00:44  dzkjz  阅读(219)  评论(0编辑  收藏  举报