我正在尝试编写 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
异常消失。谢谢大家。