【发布时间】:2022-03-11 06:25:41
【问题描述】:
我有一个新的 Laravel 8 安装,在 docker 上运行。一切似乎都运行良好,但使用任何配置的测试似乎都被破坏了。例如,将用户拉出数据库的简单测试失败。
我使用的是默认的phpunit.xml,并且我添加了一个.env.testing,它只是.env的一个副本
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
为单元 ExampleTest.php 添加了一个快速测试
/**
* A basic test example.
*
* @return void
*/
public function testBasicTest()
{
$user = User::find(1);
$this->assertInstanceOf(User::class, $user);
}
运行 sail php artisan 测试 测试:1 次失败,16 次通过 时间:2.70s
• Tests\Unit\ExampleTest > basic test
Error
Call to a member function connection() on null
at vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1493
1489▕ * @return \Illuminate\Database\Connection
1490▕ */
1491▕ public static function resolveConnection($connection = null)
1492▕ {
➜ 1493▕ return static::$resolver->connection($connection);
1494▕ }
1495▕
1496▕ /**
1497▕ * Get the connection resolver instance.
我有许多 laravel 应用程序在以前的 Laravel 版本中运行这些完全相同的测试。我错过了什么?
谢谢,
【问题讨论】:
-
在数据库部分
phpunit.xml对你说了什么?你是从哪个测试用例扩展而来的? -
同github.com/laravel/laravel/blob/8.x/phpunit.xml, 没有设置,所以我假设它应该回退到 .env.testing 文件。
-
我相信“sail php artisan test”===“sail artisan test”===“sail test”,反正我得到了同样的结果。
标签: php mysql laravel testing phpunit