【问题标题】:Resetting database once per test class in Laravel 5.4在 Laravel 5.4 中为每个测试类重置一次数据库
【发布时间】:2017-05-26 16:56:55
【问题描述】:

我正在尝试使用 Laravel 5.4 + PHPUnit 来测试我的课程。我创建了以下类来测试用户控制器:

<?php

namespace Tests\Feature;

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

class UserControllerTest extends TestCase
{
protected $baseUrl = 'http://localhost/pmv2';

use DatabaseMigrations;

public function testCreatesUser()
{
    echo "\nTest: POST /users => Create new user";

    $data = [
        'first_name'   => 'first_new_user',
        'last_name'    => 'last_new_user',
        'email'        => 'email_new@pm.com',
        'password'     => 'new_password',
        'phone_number' => '3333333333',
        'status'       => 'active',
        'created_at'   => '2000-1-1 10:10:00',
        'updated_at'   => '2000-1-1 10:10:00',
    ];

    $response = $this->post('/users', $data);
    $response->assertStatus(200);

    $this->assertDatabaseHas('users', ['email' => $data['email']]);
}

public function testReadAllUsers()
{
    $this->seed('UsersTableSeeder');

    echo "\nTest: GET /users => Read all users";

    $this->seed('UsersTableSeeder');

    $response = $this->get('/users');

    $response->assertStatus(200);
    $response->assertJson([
        'found' => true,
        'users' => [],
    ]);
}

public function testReadSingleUser()
{
    $this->seed('UsersTableSeeder');

    echo "\nTest: POST /users => Read single user";

    $response = $this->get('/users/1');

    $response->assertStatus(200);
    $response->assertJson([
        'found' => true,
        'user'  => [],
    ]);

}

public function testUpdateUser()
{
    $this->seed('UsersTableSeeder');

    echo "\nTest: POST /users => Create new user";

    $data = [
        'first_name'   => 'first_updated_user',
        'last_name'    => 'last_updated_user',
        'email'        => 'email_updated@pm.com',
        'password'     => 'updated_password',
        'phone_number' => '44444444444',
        'updated_at'   => '2000-1-1 10:10:00',
    ];

    $response = $this->put('/users/1', $data);

    $response->assertStatus(200);
    $this->assertDatabaseHas('users', ['email' => $data['email']]);
}
}

这里的问题是每次测试都会刷新数据库。在第一次测试运行之前和最后一次测试之后,我只需要刷新一次迁移。

【问题讨论】:

    标签: phpunit laravel-5.4


    【解决方案1】:

    这不是一个好主意:如果你需要这个,这意味着你的测试不是独立的,甚至执行顺序也会影响最终结果。

    那不好:你应该遵循 FIRST 原则

    • 独立/我孤独
    • R 可重复
    • 自我验证
    • 及时

    阅读更多here

    【讨论】:

      【解决方案2】:

      创建用户时会发生什么?例如,数据库中存在多一个用户。例如,您可以在 POST 调用之前和之后计算用户数。

      你真的不需要测试 POST,但是当 POST 调用被提交时会发生什么。

      而且您没有测试/user/{id} 是否存在。这使得测试依赖于一个 id:测试必须是独立的。您可以在同一个测试中放置一个 POST(以创建用户),从响应或数据库中获取最后一个 id,然后获取该用户。这是一种检查数据库中是否存在完全相同的用户的方法。并且是一种删除id的方法。如果您依赖 id,则测试只运行一次。您的测试必须是可重复的。

      每次重置数据库成本太高。我建议您执行以下步骤:

      1) 使用夹具重新创建数据库(正是您测试所需的) 2) 运行所有测试

      还请记住,测试必须很快。持续时间超过 15/20 分钟的测试套件很慢!你需要快。

      阅读测试testReadSingleUser,我建议您“手动”创建用户,检索 id,最后获取具有该 id 的用户。这使得测试可以无限重复。

      【讨论】:

        猜你喜欢
        • 2019-08-29
        • 1970-01-01
        • 2013-11-08
        • 2019-01-01
        • 2017-11-24
        • 2023-03-28
        • 1970-01-01
        • 2018-05-13
        • 1970-01-01
        相关资源
        最近更新 更多