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 ブラウザーで正しく機能します。何か案は?
更新:
これは私のルーティング ルールです。
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
そして例外は消えます。ありがとうございます。