【发布时间】:2018-05-13 05:12:03
【问题描述】:
我目前正在测试登录和注册。问题是每次测试后数据库都不会刷新,因此会导致错误。
我得到的错误:用户 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('/');
}
}
【问题讨论】: