如何从phpunit test setUp运行laravel数据库播种器?

我试图在一些PHPunit teast案例中的每个测试之前重新创建数据库.我正在使用laravel 5.3.这是TestCase:
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();
    }
}

运行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.

但这个类存在:

我将你的代码复制到我的测试项目中,我花了大约5分钟才弄明白.

问题是–class参数中的空格.如果你仔细看看数组’–class’=> ‘TestDatabaseSeeder’到底有空间……这就是问题所在.将其更改为“–class”=> ‘TestDatabaseSeeder’,它应该可以正常工作.

相关文章

[laravel] laravel的数据库配置 找到程序目录结构下.env文件 配置基本的数据库连接信息 DB_HOST=127.0....
[Laravel] Laravel的基本HTTP路由 使用Laravel的基本路由,实现get请求响应,找到文件app/Http/routes....
如果说laravel框架的核心是什么,那么无疑是服务容器。理解服务容器的概念,对于我们使用laravel太重要...
网上有很多解析laravel中间件的实现原理,但是不知道有没有读者在读的时候不明白,作者是怎么想到要用a...
laraveli添加一个或多个用户表,以admin为例。 部分文件内容可能需要根据实际情况修改 创建一个Admin模...
TL;DR: 本文介绍 Laravel 的 FastExcel 组件,文中会对 PHP generators 速览,并给出如何在节约内存的同...