小编Pat*_*der的帖子

在控制器中使用爬虫

// src/Acme/DemoBundle/Tests/Controller/DemoControllerTest.php
namespace Acme\DemoBundle\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class DemoControllerTest extends WebTestCase
{
    public function testIndex()
    {
        $client = static::createClient();

        $crawler = $client->request('GET', '/demo/hello/Fabien');

        $this->assertGreaterThan(0, $crawler->filter('html:contains("Hello Fabien")')->count());
    }
}
Run Code Online (Sandbox Code Playgroud)

这在我的测试中工作正常,但我想在控制器中也使用这个爬虫.我该怎么做?

我做路线,并添加到控制器:

<?php

// src/Ens/JobeetBundle/Controller/CategoryController

namespace Acme\DemoBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Acme\DemoBundle\Entity\Category;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class CategoryController extends Controller
{   
  public function testAction()
  {
    $client = WebTestCase::createClient();

    $crawler = $client->request('GET', '/category/index');
  }

}
Run Code Online (Sandbox Code Playgroud)

但这会给我带来错误:

Fatal error: Class 'PHPUnit_Framework_TestCase' not found in /acme/vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Test/WebTestCase.php on line 24
Run Code Online (Sandbox Code Playgroud)

php testing unit-testing symfony

6
推荐指数
1
解决办法
1922
查看次数

标签 统计

php ×1

symfony ×1

testing ×1

unit-testing ×1