所以我决定深入研究phpunit测试,在编写测试之前,我已经可耻地写出了我的php代码。无论如何,我只是写了一个非常简单的测试,告诉我是否真的找到了正确的网页。不幸的是,我的一个断言测试仍然失败。我知道我的路由“/ login”是正确的,因为当我导航到localhost / index.php / login(其中index.php是app_dev.php的链接)时,页面正确显示。 Bellow是我的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: /
答案 0 :(得分:3)
您使用“/”为导入的路线添加前缀,路径路径以“/".
开头通常情况下,我会在路线上添加更有意义的东西(并且不以“/”结尾)或从导入的路线中删除“/”。
运行以下命令可以让您深入了解路线的注册方式。
app/console debug:router