我正在尝试编写一个 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: No route found for "GET /app_dev.php/catalog/psp" ...
/app_dev.php/catalog/psp
是$categoryLink->getUri()
的动态值。和
此路线存在并在网络浏览器中正常工作。有什么想法吗?
更新:
这是我的路由规则:
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
您可以在操作中使用路由注释。
为了解决您的问题,我想查看您的路由配置。
关于php - 在 symfony 2 功能测试中找不到路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12473639/