You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用TestCase::get()报错:ReflectionException问题排查求助

问题描述

运行测试时触发以下错误:

ReflectionException: Call to undefined method PHPUnit\Framework\TestCase::get()
at vendor/pestphp/pest-plugin-laravel/src/Http.php:189
at tests/Feature/HealthTest.php:8

单元测试(无get/post/delete等HTTP请求)可正常执行,但功能测试完全无法运行,尝试多种方法无效,需定位问题原因并提供解决办法。

目录结构

  • tests目录包含:
    • Pest.php
    • TestCase.php
    • Unit(文件夹)
    • Feature(文件夹)
  • Feature文件夹内有HealthTest.php
  • 项目根目录下存在phpunit.xml

Pest.php

<?php declare(strict_types=1);
/*
|--------------------------------------------------------------------------
| Test Case
| The closure you provide to your test functions is always bound to a specific PHPUnit test
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
| need to change it using the "uses()" function to bind a different classes or traits.
|
*/

use PHPUnit\Framework\TestCase;

uses(TestCase::class)->in('Feature');
uses(TestCase::class)->in('Unit');


/*
|--------------------------------------------------------------------------
| Expectations
|--------------------------------------------------------------------------
|
| When you're writing tests, you often need to check that values meet certain conditions. The
| "expect()" function gives you access to a set of "expectations" methods that you can use
| to assert different things. Of course, you may extend the Expectation API at any time.
|
*/

expect()->extend('toBeOne', function () {
    return $this->toBe(1);
});

/*
|--------------------------------------------------------------------------
| Functions
|--------------------------------------------------------------------------
|
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
| project that you don't want to repeat in every file. Here you can also expose helpers as
| global functions to help you to reduce the number of lines of code in your test files.
|
*/

function something()
{
    // ..
}

HealthTest.php

<?php declare(strict_types=1);

//uses(Tests\TestCase::class);

use function Pest\Laravel\{get};

test('service healthy', function () {
    $response = get('/');
    $response->assertStatus(503);
});


test('pings', function (){
    $res = get('/ping', ['Device-Id'=> '3984732984792420934231']);
    $res->assertStatus(200);
});

phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         color="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         stopOnFailures="false"
         syntaxCheck="true">
    <source>
        <include>
            <directory suffix=".php">.</directory>
        </include>
    </source>
    <testsuites>
        <testsuite name="Middlewares">
            <directory phpVersion="8.3">./tests/Unit</directory>
        </testsuite>
        <testsuite name="Features">
            <directory phpVersion="8.3">./tests/Feature</directory>
        </testsuite>
    </testsuites>
</phpunit>

问题原因

核心问题是测试类绑定错误:在Pest.php中,你将Feature和Unit测试绑定到了原生PHPUnit的PHPUnit\Framework\TestCase类,但这个类不包含Laravel测试助手(如get())所需的方法和Laravel环境支持。Laravel的功能测试需要依赖项目自带的Tests\TestCase类,该类继承自Illuminate\Foundation\Testing\TestCase,内置了HTTP请求方法、应用实例初始化等必要功能。

解决办法

1. 修改Pest.php的测试类绑定

将Pest.php中的测试类引用替换为项目自带的Tests\TestCase:

// 替换原来的 use PHPUnit\Framework\TestCase;
use Tests\TestCase;

uses(TestCase::class)->in('Feature');
uses(TestCase::class)->in('Unit');

2. 验证tests/TestCase.php的正确性

确保tests目录下的TestCase.php存在且结构正确,示例如下:

<?php

namespace Tests;

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;
}

同时确保CreatesApplication trait存在(通常在tests/CreatesApplication.php),内容如下:

<?php

namespace Tests;

use Illuminate\Contracts\Console\Kernel;
use Illuminate\Foundation\Application;

trait CreatesApplication
{
    /**
     * Creates the application.
     */
    public function createApplication(): Application
    {
        $app = require __DIR__.'/../bootstrap/app.php';

        $app->make(Kernel::class)->bootstrap();

        return $app;
    }
}

3. 确保自动加载配置正确

检查项目根目录的composer.json,确认autoload-dev段包含Tests命名空间的映射:

"autoload-dev": {
    "psr-4": {
        "Tests\\": "tests/"
    }
}

如果没有,添加后执行composer dump-autoload更新自动加载缓存。

4. (可选)单个测试文件临时绑定

如果不想全局修改,可取消HealthTest.php中的注释,单独绑定测试类:

uses(Tests\TestCase::class);

完成以上步骤后,重新运行功能测试即可正常使用get()等Laravel测试助手方法。

内容的提问来源于stack exchange,提问作者Simple Fellow

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 23:47:03