2012-09-18 51 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