Symfony2 + Codeception gives “You must override the KernelTestCase::createKernel()”

不羁的心 提交于 2019-12-13 19:41:37

问题


I created a new test that inherits from WebTestCase. When I run my codeception tests, I get the following error:

 [RuntimeException] You must override the KernelTestCase::createKernel() method.

It is very unclear to me with what I need to override it. What I want is a client that can call a certain URI to get my http response

This is the test:

  public function testSearchAction()
    {
        $searchUri = '/search';
        $client = static::createClient();
        $client->request('GET', $searchUri);
        $response = $client->getResponse();
        $this->assertSame(Response::HTTP_BAD_REQUEST, $response->getStatusCode());
    }

This is my unit.suite.yml file

class_name: UnitTester

modules:
    enabled:
        - Symfony2:
            app_path: '../../app'
            var_path: '../../app'
        - Doctrine2:
            depends: Symfony2
        - AppBundle\Helper\Unit

回答1:


I just ran into the same problem when trying to integrate existing tests extending from WebTestCase into Codeception. Setting KERNEL_DIR in tests/functional/_bootstrap.php solved the issue for me:

<?php
$_SERVER['KERNEL_DIR'] = __DIR__ . '/../../app/';



回答2:


It is possible to run basics WebTestCase (or simple PHPUnit\Framework\TestCase). In your unit.suite.ym file, add the boostrap settings :

# tests/unit.suite.ym
modules:
    enabled:
        - Symfony2:
            app_path: '../../app'
            var_path: '../../app'
        - Doctrine2:
            depends: Symfony2
        - AppBundle\Helper\Unit
bootstrap: _bootstrap.php

Create the tests/unit/_bootstrap.php file

<?php
$_SERVER['KERNEL_DIR'] = __DIR__ . '/../../app/';

The loading of _bootstrap file is done by the listener Codeception\Subscriber\Bootstrap::loadBootstrap()




回答3:


Exception You must override the KernelTestCase::createKernel() method can be solved by setting KERNEL_DIR parameter in file app/phpunit.xml.dist. By default, section concerning KERNEL_DIR in this file is commented.

The full path to the kernel may look like:

<php>
    <server name="KERNEL_DIR" value="C:/symfonyProjects/project1/app" />
</php>



回答4:


If you encounter this error with Symfony 4, the solution has changed.

In Symfony 2 and 3, you could set the KERNEL_DIR in tests/functional/_bootstrap.php:

<?php
$_SERVER['KERNEL_DIR'] = __DIR__ . '/../../app/';

In Symfony 4, you should set the KERNEL_CLASS in tests/functional/_bootstrap.php:

<?php
$_SERVER['KERNEL_CLASS'] = App\Kernel::class;


来源:https://stackoverflow.com/questions/34850704/symfony2-codeception-gives-you-must-override-the-kerneltestcasecreatekernel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!