2012-09-18 47 views
3

我正在嘗試編寫一個symfony 2功能測試。這是我的代碼:在symfony 2功能測試中找不到路由

<?php 

namespace WebSite\MainBundle\Tests\Controller; 

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; 

class ProductControllerTest extends WebTestCase 
{ 

    public function testPageContents() 
    { 
     $domCategoryLinksExpr = '.catalog-col-block > ul > li > a'; 

     $client = static::createClient(); 

     $crawler = $client->request('GET', '/catalog/'); 
     $this->assertTrue($client->getResponse()->getStatusCode() == '200'); 

     $countCategories = $crawler->filter($domCategoryLinksExpr)->count(); 
     $this->assertTrue($crawler->filter($domCategoryLinksExpr)->count() > 0); 

     $categoryLink = $crawler->filter($domCategoryLinksExpr)->eq(rand(1, $countCategories))->link(); 
     $crawler = $client->click($categoryLink); 
    } 
} 

但是當我運行這個測試:

phpunit -c app src/WebSite/MainBundle/Tests/Controller/ 

我得到這個:

1)官方網站\ MainBundle \測試\控制器\ ProductControllerTest :: testPageContents Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException:未找到「GET /app_dev.php/catalog/psp」路由 ...

/app_dev.php/catalog/psp$categoryLink->getUri()的動態值。並且 此路徑存在並正確地在Web瀏覽器中工作。有任何想法嗎?

UPD:

這是我的路由規則:

routing_dev.yml: 
    ... 
    _main: 
     resource: routing.yml 
    .... 

routing.yml: 
    .... 
    WebSiteCategoryBundle: 
     resource: "@WebSiteCategoryBundle/Controller/" 
     type:  annotation 
     prefix: / 
    .... 

src/WebSite/CategoryBundle/CategoryController.php: 
    /** 
    * @Route("/catalog") 
    */ 
    class CategoryController extends Controller 
    { 
     /** 
     * @Route("/{alias}", name="show_category") 
     * @Template() 
     */ 
     public function showAction($alias) 
     { 
      // some action here 
     } 
    } 

它可以在瀏覽器中正常,但好像$ crowler does`not看到這個註釋規則。

UPD2:問題出現在Symfony 2標準版中缺少的「routing_test.yml」中。 所以我創建它:

routing_test.yml: 
    _main: 
     resource: routing_dev.yml 

和異常消失。謝謝大家。 http://symfony.com/doc/master/bundles/SensioFrameworkExtraBundle/annotations/routing.html

您可以在動作中使用的路由註釋:

+0

粘貼您的控制器的代碼和您的路由信息​​。這將有助於人們爲您提供更好的答案 –

回答

0

如果您發佈您的routing.yml

您也可以看看這將是很好。

要解決你的問題,我想看看你的路由配置。

+0

http://pastebin.com/e13zwXKV – AlexDmitriev

+1

也許這會幫助你:http://php-and-symfony.matthiasnoback.nl/2012/06/symfony2-testing- your-controllers/ –

+1

謝謝。但這是另一種測試方式,沒有WebTestCase。我想使用WebTestCase,它簡單而快速(當它工作時)。 – AlexDmitriev

0

echo $ client-> getResponse() - > getContent()將幫助您進行調試(即使有異常)。它會輸出請求的html。

它看起來像你的路線不存在,並可能在錯誤的位置(錯誤的環境指定?)

+0

輸出正確。它與http://localhost/app_dev.php/catalog uri中的相同。 – AlexDmitriev

+0

如果刪除行$ crawler = $ client-> click($ categoryLink); ?它仍然拋出異常? –

+0

不,在刪除此語句時沒有任何異常。 – AlexDmitriev