douyonglang4845 2013-07-13 05:27
浏览 53
已采纳

为什么symfony2功能测试client-> request()没有成功响应()?

So I've decided to delve into phpunit testing and I've shamefully written out my php code before writing my test. Anyway, I'm just writing a very simple test that tells me if I actually found the correct web page. Unfortunately my one assertion test keeps failing. I know my route "/login" is correct because when I navigate to localhost/index.php/login (where index.php is a link to app_dev.php), the page comes up correctly. Bellow is my routing.php file:

caremonk_mainsite_login:
    pattern:  /login
    defaults: { _controller: CaremonkMainSiteBundle:Security:login }
    requirements:
        _method: POST|GET

caremonk_mainsite_login_check:
    pattern:  /login_check
    requirements:
        _method: POST|GET

caremonk_mainsite_signup:
    pattern:  /signup
    defaults: { _controller: CaremonkMainSiteBundle:CreateUser:signup }
    requirements:
        _method: POST|GET

caremonk_mainsite_logout:
    pattern:  /logout
    defaults: { _controller: CaremonkMainSiteBundle:Security:logout}
    requirements:
        _method: POST|GET

caremonk_mainsite_post_blog:
    pattern:  /post_blog
    defaults: { _controller: CaremonkMainSiteBundle:UserEvents:post }
    requirements:
        _method: POST|GET

caremonk_mainsite_my_profile:
    pattern:  /my_profile_edit
    defaults: { _controller: CaremonkMainSiteBundle:UserEvents:editProfile }
    requirements:
        _method: POST|GET

caremonk_mainsite_activate:
    pattern:  /activate/{username}/{token}
    defaults: { _controller: CaremonkMainSiteBundle:CreateUser:activateAccount }
    requirements:
        _methods: GET

caremonk_mainsite_password_reset_request:
    pattern:  /reset_password/
    defaults: { _controller: CaremonkMainSiteBundle:Security:passwordResetRequest }
    requirements:
        _methods: GET | POST

caremonk_mainsite_reset_password_email:
    pattern: /reset_password_email/{username}/{resetPasswordToken}
    defaults: { _controller: CaremonkMainSiteBundle:Security:sendNewPassword }
    requirements:
        _methods: GET

caremonk_mainsite_change_password:
    pattern: /change_password
    defaults: { _controller: CaremonkMainSiteBundle:Security:changePassword }
    requirements:
        _methods: GET | POST

caremonk_mainsite_home:
    pattern:  /
    defaults: { _controller: CaremonkMainSiteBundle:Home:index }
    requirements:
        _methods: GET

Anyway bellow is the test code that keeps failing:

<?php

namespace Caremonk\MainSiteBundle\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class SecurityControllerFunctionalTest extends WebTestCase
{
    public function testIndex()
    {
        $client = static::createClient();

        // I've done many tests
        // I've tried the following request with all failed results
        // $crawler = $client->request('GET', 'index.php/login');
        // $crawler = $client->request('GET', 'http://localhost/indpex.php/login');
        // $crawler = $client->request('GET', 'localhost/index.php/login');
        // You get the idea 

        $crawler = $client->request('GET', '/login');
        $this->assertTrue($client->getResponse()->isSuccessful());
    }
}

My routing.yml and routing_dev.yml files are shown bellow

#routing_dev.yml
_wdt:
    resource: "@WebProfilerBundle/Resources/config/routing/wdt.xml"
    prefix:   /_wdt

_profiler:
    resource: "@WebProfilerBundle/Resources/config/routing/profiler.xml"
    prefix:   /_profiler

_configurator:
    resource: "@SensioDistributionBundle/Resources/config/routing/webconfigurator.xml"
    prefix:   /_configurator

_main:
    resource: routing.yml

#routing.yml
caremonk_main_site:
    resource: "@CaremonkMainSiteBundle/Resources/config/routing.yml"
    prefix:   /
  • 写回答

1条回答 默认 最新

  • dozoqn3347 2013-07-14 02:33
    关注

    You prefixed your imported routes with a "/", and your routes path start with a "/".

    Normally I would prefix my routes with something more meaningful (and does not end with a "/") or remove the "/" from your imported routes.

    Running the following command should give you insight to how your routes are registered.

    app/console debug:router
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀