Laravel PHPUnit在每次测试后都不会刷新

时间:2018-05-13 05:12:03

标签: php laravel phpunit

我目前正在测试登录和注册。问题是每次测试后数据库都没有刷新,因此导致错误。

  

我得到的错误:用户ID应为1,但由于数据库在每次测试后都没有重置,因此用户ID为2.我在登录时创建了一个新用户并注册了测试。

1) Tests\Unit\RegisterTest::successful
Failed asserting that a row in the table [users] matches the attributes {
    "id": 1,
    "username": "JohnRock",
    "email": "hello@email.com"
}.

Found: [
    {
        "id": 2,
        "username": "JohnRock",
        "email": "hello@email.com",
        "password": "$#^TTUG#$ORY#$&*RY#$YRY#$:RY:#$YRU$#YRP",
        "remember_token": null,
        "created_at": "2018-05-13 03:41:35",
        "updated_at": "2018-05-13 03:41:35"
    }
].
<?php

namespace Tests\Unit;

use App\User;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

class RegisterTest extends TestCase
{

    use RefreshDatabase;

    /**
     * A basic test example.
     *
     * @return void
     */


    /** @test */
    public function successful_register()
    {
        $username  = 'JohnRock';
        $email     = 'hello@email.com';
        $password  = 'fjIRHJV@#(*UH(@#*H78))';

        $user = [
            'username'              => $username,
            'email'                 => $email,
            'password'              => $password,
            'password_confirmation' => $password
        ];

        $response = $this->post('/register', $user);

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

}
<?php

namespace Tests\Unit;

use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;

class LoginTest extends TestCase
{

    use RefreshDatabase;

    /**
     * A basic test example.
     *
     * @return void
     */


    /** @test */
    public function login_successful()
    {
        $user = factory('App\User')->create();

        $response = $this->post('/login', [
            'username' => $user->username,
            'password' => $user->password
        ]);

        $response->assertRedirect('/');
    }

}

2 个答案:

答案 0 :(得分:1)

您必须查看 phpunit.xml 配置文件:如果您使用的是传统的测试数据库(不在内存数据库中),它似乎是正确的。

在 Larav.6 中,refreshTestDatabase() 被调用,在 Illuminate\Foundation\Testing\RefreshDatabase trait 中定义。

    /**
     * Refresh a conventional test database.
     *
     * @return void
     */
    protected function refreshTestDatabase()
    {
        if (! RefreshDatabaseState::$migrated) {
            $this->artisan('migrate:fresh', [
                '--drop-views' => $this->shouldDropViews(),
                '--drop-types' => $this->shouldDropTypes(),
            ]);

            $this->app[Kernel::class]->setArtisan(null);

            RefreshDatabaseState::$migrated = true;
        }

        $this->beginDatabaseTransaction();
    }

仅在第一次迭代时,当 RefreshDatabaseState::$migratedfalse 时,运行 migrate:fresh 并将 RefreshDatabaseState::$migrated 设置为 true

在后续迭代(同一类中的后续测试)中,将跳过此块。

答案 1 :(得分:0)

您的LoginTest似乎在RegistrationTest之前正在运行。由于您在那里创建了一个用户,它会自动获得id 1。

RefreshDatabase不会为每次测试重新启动,它会在启动前刷新迁移。

我建议您从id删除assertDatabaseHas,因为通过测试ID无法实现任何目标:

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