如何从 PHPUnit 测试设置运行 Laravel 数据库播种器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41790764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Run Laravel Database Seeder from PHPUnit Test setUp?
提问by gandra404
I am trying to recreate the database before each test in some PHPUnit test cases. I am using Laravel 5.3. Here is TestCase:
我试图在某些 PHPUnit 测试用例中的每个测试之前重新创建数据库。我正在使用 Laravel 5.3。这是测试用例:
class CourseTypesTest extends TestCase
{
public function setUp()
{
parent::setUp();
Artisan::call('migrate');
Artisan::call('db:seed', ['--class' => 'TestDatabaseSeeder ', '--database' => 'testing']);
}
/**
* A basic functional test example.
*
* @return void
*/
public function test_list_course_types()
{
$httpRequest = $this->json('GET', '/api/course-types');
$httpRequest->assertResponseOk();
$httpRequest->seeJson();
}
public function tearDown()
{
Artisan::call('migrate:reset');
parent::tearDown();
}
}
Running phpunit
fails with error:
运行phpunit
失败并出现错误:
$ phpunit PHPUnit 5.7.5 by Sebastian Bergmann and contributors.
E 1 / 1 (100%)
Time: 2.19 seconds, Memory: 12.00MB
There was 1 error:
1) CourseTypesTest::test_list_course_types ReflectionException: Class TestDatabaseSeeder does not exist
D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Container\Container.php:749 D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Container\Container.php:644 D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Foundation\Application.php:709 D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Database\Console\Seeds\SeedCommand.php:74 D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Database\Console\Seeds\SeedCommand.php:63 D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:2292 D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Database\Console\Seeds\SeedCommand.php:64 D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Container\Container.php:508 D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Console\Command.php:169 D:\www\learn-laravel\my-folder-api\vendor\symfony\console\Command\Command.php:254 D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Console\Command.php:155 D:\www\learn-laravel\my-folder-api\vendor\symfony\console\Application.php:821 D:\www\learn-laravel\my-folder-api\vendor\symfony\console\Application.php:187 D:\www\learn-laravel\my-folder-api\vendor\symfony\console\Application.php:118 D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Console\Application.php:107 D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Foundation\Console\Kernel.php:218 D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php:237 D:\www\learn-laravel\my-folder-api\tests\rest\CourseTypesTest.php:17
ERRORS! Tests: 1, Assertions: 0, Errors: 1.
$ phpunit PHPUnit 5.7.5 由 Sebastian Bergmann 和贡献者编写。
E 1 / 1 (100%)
时间:2.19 秒,内存:12.00MB
有 1 个错误:
1) CourseTypesTest::test_list_course_types ReflectionException: Class TestDatabaseSeeder 不存在
D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Container\Container.php:749 D:\www\learn-laravel\my-folder-api\vendor\laravel \framework\src\Illuminate\Container\Container.php:644 D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Foundation\Application.php:709 D:\www \learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Database\Console\Seeds\SeedCommand.php:74 D:\www\learn-laravel\my-folder-api\vendor\laravel \framework\src\Illuminate\Database\Console\Seeds\SeedCommand.php:63 D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php :2292 D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Database\Console\Seeds\SeedCommand.php:64 D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Container\Container.php:508 D:\www\learn-laravel\my-folder-api\vendor\laravel\framework \src\Illuminate\Console\Command.php:169 D:\www\learn-laravel\my-folder-api\vendor\symfony\console\Command\Command.php:254 D:\www\learn-laravel\my -folder-api\vendor\laravel\framework\src\Illuminate\Console\Command.php:155 D:\www\learn-laravel\my-folder-api\vendor\symfony\console\Application.php:821 D: \www\learn-laravel\my-folder-api\vendor\symfony\console\Application.php:187 D:\www\learn-laravel\my-folder-api\vendor\symfony\console\Application.php:118 D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Console\Application.php:107 D:\www\learn-laravel\my-folder-api\vendor\laravel \framework\src\Illuminate\Foundation\Console\Kernel.php:218 D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php:237 D:\www\learn-laravel\my-folder-api\tests\rest \CourseTypesTest.php:17
错误!测试:1,断言:0,错误:1。
采纳答案by Bostjan
The problem is empty space in your --class argument. If you take close look at array '--class' => 'TestDatabaseSeeder '
there is space in the end ... this is the problem. Change it to '--class' => 'TestDatabaseSeeder'
and it should work fine.
问题是您的 --class 参数中的空白空间。如果你仔细看看数组'--class' => 'TestDatabaseSeeder '
,到底有没有空间……这就是问题所在。将其更改为'--class' => 'TestDatabaseSeeder'
,它应该可以正常工作。
回答by Guillaume Boutin
The DatabaseSeeder
can be instantiated on its own, and its call
method is public.
该DatabaseSeeder
可自行实例化,其call
方法是公共的。
All you need to do in your CourseTypesTest
class would be
你在CourseTypesTest
课堂上需要做的就是
(new DatabaseSeeder())->call(TestDatabaseSeeder::class);
Or you can make use of Laravel's app
helper as follow
或者你可以使用 Laravel 的app
助手如下
app(DatabaseSeeder::class)->call(TestDatabaseSeeder::class);
回答by Santi Barbat
Since version 5.8 you can do something like:
从 5.8 版开始,您可以执行以下操作:
// Run the DatabaseSeeder...
$this->seed();
// Run a single seeder...
$this->seed(OrderStatusesTableSeeder::class);