4

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

そして例外は消えます。ありがとうございます。

4

2 に答える 2

1

routing.yml を投稿していただければ幸いです

以下もご覧ください: http://symfony.com/doc/master/bundles/SensioFrameworkExtraBundle/annotations/routing.html

アクションでルーティング アノテーションを使用できます。

問題を解決するために、ルーティング構成を確認したいと思います。

于 2012-09-18T09:05:41.713 に答える
1

echo $client->getResponse()->getContent() はデバッグに役立ちます (例外があっても)。リクエストのhtmlを出力します。

ルートが存在しないようで、間違った場所にある可能性があります (間違った環境が指定されていますか?)

于 2012-09-18T09:22:01.553 に答える