Pphunit 不断给出错误:[1049] 未知数据库':内存:'

Posted

技术标签:

【中文标题】Pphunit 不断给出错误:[1049] 未知数据库\':内存:\'【英文标题】:Pphunit keeps giving error: [1049] Unknown database ':memory:'Pphunit 不断给出错误:[1049] 未知数据库':内存:' 【发布时间】:2020-10-30 13:53:07 【问题描述】:

我刚开始在 Laravel 7 中使用 phpunit。我遇到了一个我找不到解决方案的问题。我正在使用 Laravel 7 和 Xampp。

My phpunit.xml:

    <?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpcd ..
         cdit/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         colors="true"
>
    <testsuites>
        <testsuite name="Unit">
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>
        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
        </whitelist>
    </filter>
    <php>
        <server name="APP_ENV" value="testing"/>
        <server name="BCRYPT_ROUNDS" value="4"/>
        <server name="CACHE_DRIVER" value="array"/>
        <server name="DB_CONNECTION" value="mysql"/>
        <server name="DB_DATABASE" value=":memory:"/>
        <server name="MAIL_MAILER" value="array"/>
        <server name="QUEUE_CONNECTION" value="sync"/>
        <server name="SESSION_DRIVER" value="array"/>
        <server name="TELESCOPE_ENABLED" value="false"/>
    </php>
</phpunit>

我的测试功能:

<?php

namespace Tests\Feature;


use Illuminate\Foundation\Testing\DatabaseMigrations;


use Tests\TestCase;

class ThreadsTest extends TestCase


    use DatabaseMigrations;

    public function test_a_user_can_browse_threads()
    
        $response = $this->get('/threads');

        $response->assertStatus(200);
    

当我运行 phpunit (vendor/phpunit/phpunit/phpunit) 我得到这个:

PHPUnit 8.5.8 by Sebastian Bergmann and contributors.

.E                                                                  2 / 2 (100%)

Time: 1.57 seconds, Memory: 20.00 MB

There was 1 error:

1) Tests\Feature\ThreadsTest::test_a_user_can_browse_threads
Illuminate\Database\QueryException: SQLSTATE[HY000] [1049] Unknown database ':memory:' (SQL: 

显示完整的表,其中 table_type = 'BASE TABLE')

C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Database\Connection.php:671
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Database\Connection.php:631
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Database\Connection.php:339
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Database\Schema\MySqlBuilder.php:99
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Database\Schema\MySqlBuilder.php:48
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Database\Console\WipeCommand.php:69
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Database\Console\WipeCommand.php:46
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:33
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Container\Util.php:37
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:91
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:35
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Container\Container.php:592
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Console\Command.php:134
C:\xampp\htdocs\forum\vendor\symfony\console\Command\Command.php:258
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Console\Command.php:121
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Console\Concerns\CallsCommands.php:56
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Console\Concerns\CallsCommands.php:28
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Database\Console\Migrations\FreshCommand.php:41
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:33
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Container\Util.php:37
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:91
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:35
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Container\Container.php:592
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Console\Command.php:134
C:\xampp\htdocs\forum\vendor\symfony\console\Command\Command.php:258
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Console\Command.php:121
C:\xampp\htdocs\forum\vendor\symfony\console\Application.php:911
C:\xampp\htdocs\forum\vendor\symfony\console\Application.php:264
C:\xampp\htdocs\forum\vendor\symfony\console\Application.php:140
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Console\Application.php:93
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Console\Application.php:185
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Foundation\Console\Kernel.php:263
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Testing\PendingCommand.php:171
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Testing\PendingCommand.php:291
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\InteractsWithConsole.php:52
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Foundation\Testing\DatabaseMigrations.php:16
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Foundation\Testing\TestCase.php:119
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Foundation\Testing\TestCase.php:84

Caused by
PDOException: SQLSTATE[HY000] [1049] Unknown database ':memory:'

C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:46
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Database\Connectors\MySqlConnector.php:24
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Database\Connectors\ConnectionFactory.php:184
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Database\Connection.php:926
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Database\Connection.php:961
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Database\Connection.php:405
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Database\Connection.php:331
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Database\Connection.php:631
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Database\Connection.php:339
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Database\Schema\MySqlBuilder.php:99
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Database\Schema\MySqlBuilder.php:48
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Database\Console\WipeCommand.php:69
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Database\Console\WipeCommand.php:46
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:33
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Container\Util.php:37
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:91
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:35
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Container\Container.php:592
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Console\Command.php:134
C:\xampp\htdocs\forum\vendor\symfony\console\Command\Command.php:258
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Console\Command.php:121
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Console\Concerns\CallsCommands.php:56
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Console\Concerns\CallsCommands.php:28
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Database\Console\Migrations\FreshCommand.php:41
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:33
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Container\Util.php:37
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:91
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:35
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Container\Container.php:592
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Console\Command.php:134
C:\xampp\htdocs\forum\vendor\symfony\console\Command\Command.php:258
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Console\Command.php:121
C:\xampp\htdocs\forum\vendor\symfony\console\Application.php:911
C:\xampp\htdocs\forum\vendor\symfony\console\Application.php:264
C:\xampp\htdocs\forum\vendor\symfony\console\Application.php:140
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Console\Application.php:93
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Console\Application.php:185
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Foundation\Console\Kernel.php:263
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Testing\PendingCommand.php:171
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Testing\PendingCommand.php:291
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\InteractsWithConsole.php:52
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Foundation\Testing\DatabaseMigrations.php:16
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Foundation\Testing\TestCase.php:119
C:\xampp\htdocs\forum\vendor\laravel\framework\src\Illuminate\Foundation\Testing\TestCase.php:84

ERRORS!
Tests: 2, Assertions: 1, Errors: 1.

我的MYSQL数据库中的表也都被删除了。

有什么建议吗?

亲切的问候,

休伯特

【问题讨论】:

将 Mysql 更改为 Sqlite 似乎可以解决问题。但我不明白为什么。据我所知,我的机器上没有 Sqlite!你能澄清一下吗? 【参考方案1】:

我猜你想将sqlite 作为内存数据库运行,至少这是我首选的测试设置。

要实现这一点,您必须像这样在测试中设置环境。现在你的连接是Mysql,这不能使用sqlite的内存设置。

<server name="DB_CONNECTION" value="sqlite"/>
<server name="DB_DATABASE" value=":memory:"/>

【讨论】:

以上是关于Pphunit 不断给出错误:[1049] 未知数据库':内存:'的主要内容,如果未能解决你的问题,请参考以下文章

如何处理这个错误(1049,“未知数据库'/users/ohyunjun/work/astral/mysql'”)

SQLSTATE[HY000] [1049] 未知数据库 'laravel'

Laravel 迁移未知数据库 [1049]

pymysql.err.InternalError:(1049,“未知数据库”)

警告:mysqli_connect(): (HY000/1049): 仅在 mac 终端中的未知数据库

Laravel PDOException SQLSTATE[HY000] [1049] 未知数据库 'forge'