SQLSTATE[42S02]:未找到基表或视图:1146 表“testing.users”不存在(连接:mysql,SQL:选择 count(*) 作为聚合来自

问题描述 投票:0回答:2

我正在尝试测试 pest 中的用户注册,这是我的 pest 代码

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

am getting this error
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` = [email protected])   

  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▕

在 docker 上,所有容器都运行良好。并且我的数据库中有用户表,当我测试与数据库或 mysql 相关的某些内容时,出现错误

这是我的

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' => '[email protected]', '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' => '[email protected]', 'first_name' => 'vishal', 'last_name' => 'singh', ]);
});
laravel database docker testing pestphp
2个回答
0
投票

Laravel Sail 创建一个名为 testing 的专用数据库,以便您的测试与主数据库隔离,因此不会干扰其中的数据(来源:https://laravel.com/docs/11. x/sail#running-tests)。 Sail 还会修改项目中的 phpunit.xml 文件,以在运行测试时指向此 testing 数据库,通过添加这行代码...

<env name="DB_DATABASE" value="testing"/>

您报告的错误消息指出

Base table or view not found: 1146 Table 'testing.users' doesn't exist

如果您检查 testing 数据库(例如使用 MySQL Workbench),您将看到它是一个空数据库,这与您收到的错误消息一致,即

testing 中不存在 users 表数据库。事实上,您的主数据库中有一个 
users
表是无关紧要的,因为您的 Pest 测试未设置为查看您的主数据库。

一种解决方案是从您的 phpunit.xml 文件中删除上述代码行(正如您在答案中所指出的那样)。但是,我不会推荐这种方法,因为您的测试会污染您的主数据库,并且可能会无意中删除您想要保留的数据。

显然,如果 testing 数据库为空,则需要在测试运行之前运行迁移以创建适当的表。有用的是,Pest 提供了一个配置选项,启用该选项后,将在每次测试之前重置数据库。这本质上是在您的

testing 数据库上调用 php artisan migrate:fresh

因此,

最好的解决方案是编辑您的 tests/Pest.php 文件并取消注释 uses() 函数中将

Illuminate\Foundation\Testing\RefreshDatabase
类绑定到测试函数的行:
/*
|--------------------------------------------------------------------------
| Test Case
|--------------------------------------------------------------------------
|
| The closure you provide to your test functions is always bound to a specific PHPUnit test
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
| need to change it using the "uses()" function to bind a different classes or traits.
|
*/

uses(
    Tests\TestCase::class,
    Illuminate\Foundation\Testing\RefreshDatabase::class,  // <- uncommented line
)->in('Feature');



-1
投票

© www.soinside.com 2019 - 2024. All rights reserved.