加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-14 19:49:12 所属栏目:大数据 来源:网络整理
导读:我试图在一些phpunit teast案例中的每个测试之前重新创建数据库.我正在使用laravel 5.3.这是TestCase: class CourseTypesTest extends TestCase{ public function setUp() { parent::setUp(); Artisan::call('migrate'); Artisan::call('db:seed',['--class
我试图在一些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:wwwlearn-laravelmy-folder-apivendorlaravelframeworksrcIlluminateContainerContainer.php:749
D:wwwlearn-laravelmy-folder-apivendorlaravelframeworksrcIlluminateContainerContainer.php:644
D:wwwlearn-laravelmy-folder-apivendorlaravelframeworksrcIlluminateFoundationApplication.php:709
D:wwwlearn-laravelmy-folder-apivendorlaravelframeworksrcIlluminateDatabaseConsoleSeedsSeedCommand.php:74
D:wwwlearn-laravelmy-folder-apivendorlaravelframeworksrcIlluminateDatabaseConsoleSeedsSeedCommand.php:63
D:wwwlearn-laravelmy-folder-apivendorlaravelframeworksrcIlluminateDatabaseEloquentModel.php:2292
D:wwwlearn-laravelmy-folder-apivendorlaravelframeworksrcIlluminateDatabaseConsoleSeedsSeedCommand.php:64
D:wwwlearn-laravelmy-folder-apivendorlaravelframeworksrcIlluminateContainerContainer.php:508
D:wwwlearn-laravelmy-folder-apivendorlaravelframeworksrcIlluminateConsoleCommand.php:169
D:wwwlearn-laravelmy-folder-apivendorsymfonyconsoleCommandCommand.php:254
D:wwwlearn-laravelmy-folder-apivendorlaravelframeworksrcIlluminateConsoleCommand.php:155
D:wwwlearn-laravelmy-folder-apivendorsymfonyconsoleApplication.php:821
D:wwwlearn-laravelmy-folder-apivendorsymfonyconsoleApplication.php:187
D:wwwlearn-laravelmy-folder-apivendorsymfonyconsoleApplication.php:118
D:wwwlearn-laravelmy-folder-apivendorlaravelframeworksrcIlluminateConsoleApplication.php:107
D:wwwlearn-laravelmy-folder-apivendorlaravelframeworksrcIlluminateFoundationConsoleKernel.php:218
D:wwwlearn-laravelmy-folder-apivendorlaravelframeworksrcIlluminateSupportFacadesFacade.php:237
D:wwwlearn-laravelmy-folder-apitestsrestCourseTypesTest.php:17

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

但这个类存在:

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读