我正在尝试编写一个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
您可以在动作中使用的路由注释:
粘贴您的控制器的代码和您的路由信息。这将有助于人们为您提供更好的答案 –