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

Drupal PHPUnit测试中使用Url::fromRoute报路由不存在的解决办法

解决Drupal测试中Route "commerce_payment.checkout.return"不存在的问题

问题重现

测试类代码:

class ReturnHandlerTest extends OrderKernelTestBase
{
    protected static $modules = [
      'commerce_payment',
      'commerce_payment_example',
    ];

    protected function setUp(): void {
        parent::setUp();
    }
}

测试中调用的代码:

Url::fromRoute('commerce_payment.checkout.return', $arguments)
    ->setOptions(['absolute' => true])
    ->toString()

抛出的异常信息:

Symfony\Component\Routing\Exception\RouteNotFoundException: Route "commerce_payment.checkout.return" does not exist.

解决方法

1. 补充缺失的依赖模块

commerce_payment.checkout.return路由属于commerce_checkout模块,你的测试类当前未启用该模块,导致路由无法被识别。只需在测试类的static $modules数组中添加commerce_checkout即可:

修改后的测试类代码:

class ReturnHandlerTest extends OrderKernelTestBase
{
    protected static $modules = [
      'commerce_payment',
      'commerce_payment_example',
      'commerce_checkout', // 新增依赖模块
    ];

    protected function setUp(): void {
        parent::setUp();
    }
}

2. 额外验证点

  • 确认OrderKernelTestBase是否默认启用了commerce_order模块(通常该基类会包含此依赖,若仍有异常可手动添加到模块列表)
  • 若问题持续,可在本地Drupal环境执行drush route-list | grep commerce_payment.checkout.return,确认路由所属模块,确保测试用例中已启用对应模块

内容的提问来源于stack exchange,提问作者sk DYLAN

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 01:50:25