You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel Pest用户注册测试报错:users表不存在问题求助

Laravel Pest测试用户注册时SQL表不存在错误排查

问题描述

使用Pest测试Laravel用户注册功能时,触发SQLSTATE[42S02]错误,提示testing.users表不存在。Docker所有容器运行正常,且数据库中已存在users表,但所有涉及数据库的测试均报错。


测试代码

test('signup user', function () {
    $response = $this->post('signup-user',[
        'email' => 'vishal444@gmail.com',
        'first_name' => 'vishal',
        'last_name' => 'singh',
        'password' => '111111',
        'confirm_password' => '111111'
    ]);
        
    $response->assertSessionHas('success', 'You have registered successfully');  
});

报错信息

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'testing.users' doesn't exist (Connection: mysql, SQL: select count(*) as aggregate from `users` where `email` = vishal444@gmail.com)   

  at D:\laravel-tailwind\laravelwithtailwind\tests\Feature\AppTest.php:36
     32▕         'password' => '111111',
     33▕         'confirm_password' => '111111'
     34▕     ]);
     35▕
  ➜  36▕     $response->assertSessionHas('success', 'You have registered successfully');
     37▕ });
     38▕

signup_user控制器函数代码

public function signupUser(Request $request) {
    $request->validate([
        'email'=>'required|email|unique:users',
        'first_name'=>'required', 
        'last_name'=>'required', 
        'password'=>'required|min:6|max:12', 
        'confirm_password'=>'required|min:6|max:12'
    ]);
    $user = new User(); 
    $user->email = $request->email; 
    $user->first_name = $request->first_name; 
    $user->last_name = $request->last_name; 
    $user->password = Hash::make($request->password); 
    $user->confirm_password = Hash::make($request->confirm_password);
}

目标测试用例

test('user can signup', function () {
    $userData = [ 'email' => 'vishal123@gmail.com', 'first_name' => 'vishal', 'last_name' => 'singh', 'password' => '123123', 'confirm_password' => '123123', ];
    $this->post(route('signup-user'), $userData)
         ->assertSessionHas('success', 'You have registered successfully');
    $this->assertDatabaseHas('users', [ 'email' => 'vishal123@gmail.com', 'first_name' => 'vishal', 'last_name' => 'singh', ]);
});

排查解决方案

  1. 确认测试环境数据库配置

    • 检查.env.testing文件,确保DB_DATABASE设为testing,且DB_HOST、DB_PORT、DB_USERNAME、DB_PASSWORD与Docker中MySQL容器的配置一致。
    • 若无.env.testing文件,复制.env并重命名为.env.testing,修改数据库配置为测试专用库。
  2. 执行测试环境数据库迁移

    • Laravel测试默认使用独立的测试数据库,需单独执行迁移命令:
      php artisan migrate --env=testing
      
    • 若使用Docker,需进入容器内部执行:
      docker exec -it [容器名称/ID] php artisan migrate --env=testing
      
  3. 启用测试自动迁移特性

    • 在tests/TestCase.php中引入RefreshDatabase trait,它会在每个测试前自动迁移并回滚,保证测试环境数据库结构最新:
      use Illuminate\Foundation\Testing\RefreshDatabase;
      
      class TestCase extends BaseTestCase
      {
          use RefreshDatabase;
          // ...
      }
      
  4. 验证测试数据库连接状态

    • 可在测试中临时添加代码,确认是否能正确访问testing数据库:
      test('check database connection', function () {
          $connection = DB::connection();
          dump($connection->getDatabaseName()); // 应输出testing
          dump($connection->getTables()); // 查看是否存在users表
      });
      
  5. 补全控制器保存逻辑

    • 提供的signupUser函数缺少用户保存代码,需添加$user->save();,否则用户数据不会写入数据库,后续断言也会失败:
      public function signupUser(Request $request) {
          // ... 原有验证与赋值逻辑 ...
          $user->confirm_password = Hash::make($request->confirm_password);
          $user->save(); // 添加此行
          return redirect()->back()->with('success', 'You have registered successfully');
      }
      

内容的提问来源于stack exchange,提问作者Vishal Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 07:25:33