【发布时间】:2018-01-17 17:29:18
【问题描述】:
我遇到了错误
致命错误:在中找不到类“TestCase” /home/vagrant/Code/myapp/tests/ExampleTest.php 在第 7 行
在运行命令 ./vendor/bin/phpunit 时。我阅读了有关此错误的信息并更改了我的 composer.json 并添加了 TestCase.php 文件。但我仍然收到此错误。
我的文件:
composer.json
"autoload-dev": {
"classmap": [
"tests/TestCase.php"
],
"psr-4": {
"Tests\\": "tests/"
}
}
TestCase.php
<?php
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
}
ExampleTest.php
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testExample()
{
$this->assertTrue(true);
}
}
【问题讨论】:
-
你运行的是哪个版本的 Laravel 5?您可以使用
php artisan --version检查您正在使用的版本。在 5.4.30 上,示例单元测试还有一个use Tests\TestCase;语句并命名为namespace Tests\Unit; -
@Chris Forrence 我使用的是 5.3.31 版本
-
您拥有的 TestCase.php 文件与 Laravel 5.4 的格式匹配(具有命名空间和特征用法);如果 ExampleTest.php 和 TestCase.php 在同一个文件夹中,则将
namespace Tests;添加到 ExampleTest.php 文件的顶部 -
@Chris Forrence 一切正常,谢谢。
标签: php json laravel laravel-5