首次使用Codeception测试带参数Symfony路由失败,求解决方法
测试带参数的Symfony路由(Codeception)
先梳理你代码里的问题,再给出正确的测试方案:
1. 修正路由注解的语法错误
你的路由注解存在字符串未加引号、名称含空格的问题,建议统一参数拼写(比如plateform改为platform避免混淆),修正后如下:
#[Route('/saisie/positer_particuliere/{platform}/{dayNb}-{weekNb}-{year}', name: 'app_saisie_positer_particulieres')]
2. 测试带参数路由的正确方式
不要硬拼URL路径,Codeception的Symfony模块提供了amOnRoute方法,直接通过路由名称+参数数组生成正确URL,比硬拼更可靠,也能避免路径拼写错误。
3. 修正测试方法的错误
你的测试方法存在异常捕获语法错误、方法名拼写错误、断言使用不当等问题,修正后的完整代码:
public function jeVaisSurLeLienSaisiePositerParticuliere() { $date_now = new DateTimeImmutable(); // 整理路由所需参数 $routeParams = [ 'platform' => 1, 'dayNb' => $date_now->format("N"), 'weekNb' => $date_now->format("W"), 'year' => $date_now->format("Y") ]; // 通过路由名称跳转,自动生成符合规则的URL $this->I->amOnRoute('app_saisie_positer_particulieres', $routeParams); // 按ID点击元素,无需重复传递文本和选择器 $this->I->click('#1c-plateforme'); // 断言页面存在指定select元素,用seeElement比see更准确 $this->I->seeElement('select[name="plateforme"]'); }
额外注意事项
- 确保你的Codeception测试套件(如Acceptance)已启用Symfony模块,在对应
*.suite.yml配置中添加:modules: enabled: - Symfony - PhpBrowser - 若测试仍失败,可添加
$this->I->seePageUrl()输出当前页面URL,对比预期路径排查参数或路由匹配问题 - 移除不必要的try-catch,Codeception会自动捕获测试错误并输出详细日志,更便于定位问题
内容的提问来源于stack exchange,提问作者user19437292
相关产品推荐
相关产品推荐

