2013-07-13 71 views
0

所以我決定深入研究phpunit測試,並且在編寫測試之前我可恥地寫出了我的php代碼。無論如何,我只是寫了一個非常簡單的測試,告訴我是否真的找到了正確的網頁。不幸的是,我的一個斷言測試一直在失敗。我知道我的路由「/ login」是正確的,因爲當我導航到localhost/index.php/login(其中index.php是app_dev.php的鏈接)時,頁面正確顯示。貝婁是我routing.php文件:爲什麼symfony2函數測試client-> request()沒有獲得成功的響應()?

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

檢查內容與'$客戶 - > GETRESPONSE() - >用'$客戶 - > GETRESPONSE()的getContent()'和狀態代碼 - > getStatusCode()' –

+0

響應代碼是404 –

+0

MB你在'routes_dev.yml'文件中定義路由? –

回答

3

您使用「/」前綴導入的路線,並且您的路線路徑以「/」開頭。

通常我會在我的路由前加一些更有意義的東西(並且不以「/」結尾)或從導入的路由中刪除「/」。

運行以下命令可以讓您瞭解路由的註冊方式。

app/console debug:router 
相關問題