[英]no route found in symfony 2 functional test
我正在嘗試編寫一個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)WebSite \\ MainBundle \\ Tests \\ Controller \\ 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確實沒有看到這個注釋規則。
UPD2:問題出現在Symfony 2標准版中缺少的“routing_test.yml”中。 所以我創建它:
routing_test.yml:
_main:
resource: routing_dev.yml
和異常消失。 謝謝大家。
如果您發布了routing.yml,那就太好了
您還可以查看: http : //symfony.com/doc/master/bundles/SensioFrameworkExtraBundle/annotations/routing.html
您可以在操作中使用路由注釋。
為了解決您的問題,我希望看到您的路由配置。
echo $ client-> getResponse() - > getContent()將幫助您進行調試(即使有例外)。 它將輸出請求的html。
看起來您的路線不存在,可能位於錯誤的位置(指定了錯誤的環境?)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.