我试图在一些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’,它应该可以正常工作.