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

前端之家收集整理的这篇文章主要介绍了如何从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相关文章