0

所以我决定深入研究 phpunit 测试,并且在编写测试之前我已经羞耻地写出了我的 php 代码。无论如何,我只是在编写一个非常简单的测试,告诉我是否真的找到了正确的网页。不幸的是,我的一个断言测试一直失败。我知道我的路线“/login”是正确的,因为当我导航到 localhost/index.php/login(其中 index.php 是指向 app_dev.php 的链接)时,页面会正确显示。贝娄是我的 routing.php 文件:

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

无论如何,下面是不断失败的测试代码:

<?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());
    }
}

我的 routing.yml 和 routing_dev.yml 文件如下所示

#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:   /
4

1 回答 1

3

您为导入的路由添加了前缀“/”,并且您的路由路径以“/”开头。

通常我会在我的路线前面加上更有意义的东西(并且不以“/”结尾)或从导入的路线中删除“/”。

运行以下命令应该可以让您深入了解您的路线是如何注册的。

app/console debug:router
于 2013-07-14T02:33:55.433 回答