Error in laravel testing : SQLSTATE[HY000]: General error: 1 no such table: users

Error in laravel testing : SQLSTATE[HY000]: General error: 1 no such table: users

当我在 laravel 中使用 PHPUnit 并将工厂添加到我自己的测试 class 之后 运行 PHPUnit 在控制台中出现此错误:

 SQLSTATE[HY000]: General error: 1 no such table: users 

我的测试class方法:

public function testExistSomeTextsInIndexPage()
{
    $users= factory(User::class)->create();
    $this->get('/')->assertSee($users->name);
     
}

这个工厂代码在我项目的其他部分工作正常只是在测试中显示错误class

您应该将 Illuminate\Foundation\Testing\DatabaseMigrations 作为特征包括在内。关于这个有documentation

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

class ExampleTest extends TestCase
{
    use DatabaseMigrations;
}

注意:设置正确的数据库凭据。如果您将凭据用于现有数据库,它将覆盖数据库。我建议您使用带有 SQLite 的 in-memory 数据库,请参见示例