【问题标题】:Laravel 5: Fatal error: Class 'TestCase' not foundLaravel 5:致命错误:找不到类“TestCase”
【发布时间】: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


【解决方案1】:

您应该将命名空间添加到您的 ExampleTest 类:

namespace Tests;

【讨论】:

    猜你喜欢
    • 2016-02-08
    • 2016-04-09
    • 2016-01-28
    • 2014-10-02
    • 2014-03-10
    • 2013-08-12
    • 2016-08-08
    • 2016-02-07
    • 1970-01-01
    相关资源
    最近更新 更多