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

Symfony 6测试请求找不到路由的问题求助

Symfony 6 WebTestCase 404错误排查与解决

我基于Symfony 6编写WebTestCase,待测试的控制器能正常返回JSON格式数组数据,但测试时始终报404错误。实际请求的有效URL为http://masch1/company/public/get/data?active=1&sort=desc,按官方文档尝试多种路径组合(带域名、去掉域名的不同路径)均无效。

测试代码

<?php

// php bin/phpunit tests/Controller/DataControllerTest.php
namespace App\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class DataControllerTest extends WebTestCase
{
    public function testSomething(): void
    {
        $client = static::createClient();
        $crawler = $client->request('GET', 'http://masch1/company/public/get/data?active=1&sort=desc');

        $this->assertResponseIsSuccessful();
        $this->assertSelectorTextContains('h1', 'Hello World');
    }
}

错误信息

/var/www/company/vendor/symfony/framework-bundle/Test/BrowserKitAssertionsTrait.php:142
/var/www/company/vendor/symfony/framework-bundle/Test/BrowserKitAssertionsTrait.php:33
/var/www/company/tests/Controller/DataControllerTest.php:15

Caused by ErrorException: No route found for "GET http://localhost/company/public/get/data" in /var/www/company/vendor/symfony/http-kernel/EventListener/RouterListener.php:127
Stack trace:
#0 /var/www/company/vendor/symfony/framework-bundle/Test/BrowserKitAssertionsTrait.php(33): Symfony\Bundle\FrameworkBundle\Test\WebTestCase::assertThatForResponse()
#1 /var/www/company/tests/Controller/DataControllerTest.php(15): Symfony\Bundle\FrameworkBundle\Test\WebTestCase::assertResponseIsSuccessful()
#2 /var/www/company/vendor/phpunit/phpunit/src/Framework/TestCase.php(1608): App\Tests\Controller\DataControllerTest->testSomething()
#3 /var/www/company/vendor/phpunit/phpunit/src/Framework/TestCase.php(1214): PHPUnit\Framework\TestCase->runTest()
#4 /var/www/company/vendor/phpunit/phpunit/src/Framework/TestResult.php(728): PHPUnit\Framework\TestCase->runBare()
#5 /var/www/company/vendor/phpunit/phpunit/src/Framework/TestCase.php(964): PHPUnit\Framework\TestResult->run()
#6 /var/www/company/vendor/phpunit/phpunit/src/Framework/TestSuite.php(684): PHPUnit\Framework\TestCase->run()
#7 /var/www/company/vendor/phpunit/phpunit/src/TextUI/TestRunner.php(651): PHPUnit\Framework\TestSuite->run()
#8 /var/www/company/vendor/phpunit/phpunit/src/TextUI/Command.php(144): PHPUnit\TextUI\TestRunner->run()
#9 /var/www/company/vendor/phpunit/phpunit/src/TextUI/Command.php(97): PHPUnit\TextUI\Command->run()
#10 /var/www/company/bin/phpunit(11): PHPUnit\TextUI\Command::main()
#11 {main}

解决方法

  • 使用路由路径而非完整URL:Symfony测试客户端不需要域名和public目录前缀,只需传入路由的路径部分。修改请求代码为:
    $crawler = $client->request('GET', '/get/data', [
        'active' => 1,
        'sort' => 'desc'
    ]);
    
  • 修正错误断言:控制器返回JSON数据,页面不存在h1标签,替换为JSON相关断言:
    $this->assertResponseContentTypeSame('application/json');
    $data = json_decode($client->getResponse()->getContent(), true);
    $this->assertIsArray($data);
    
  • 验证路由配置:执行php bin/console debug:router命令,确认/get/data路由存在且正确映射到目标控制器方法。
  • 检查测试环境路由:确认config/packages/test/routes.yaml中没有覆盖或禁用该路由。

内容的提问来源于stack exchange,提问作者Juergen Schulze

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 21:44:56