如果“framework.test”配置未设置为 true,则无法创建功能测试中使用的客户端

问题描述 投票:0回答:2

我正在尝试使用 WebTestCase 来应用功能测试。

class CalculatorControllerTest extends WebTestCase
{
    public function testSumPost()
    {
        $client = static::createClient();

        $client->request('GET', '/calculator/sum');

        $this->assertEquals(200, $client->getResponse()->getStatusCode());
    }
}

运行测试时出现以下错误消息:

 You cannot create the client used in functional tests if the "framework.test" config is not set to true

我的测试框架:

framework:
 test: ~
 session:
   storage_id: session.storage.mock_file
 router:
   resource: "%kernel.root_dir%/config/routing_test.yml"
  profiler:
   collect: false

我的 phpunit.xml:(我也尝试在 APP_ENV 中测试而不是 dev & 调试 1 而不是 0)

<?xml version="1.0" encoding="UTF-8"?>

<phpunit
    backupGlobals               = "false"
    backupStaticAttributes      = "false"
    colors                      = "true"
    convertErrorsToExceptions   = "true"
    convertNoticesToExceptions  = "true"
    convertWarningsToExceptions = "true"
    processIsolation            = "false"
    stopOnFailure               = "false"
    bootstrap                   = "config/bootstrap.php" >

<php>
    <ini name="error_reporting" value="-1" />
    <server name="KERNEL_CLASS" value="AppKernel" />
    <env name="APP_ENV" value="dev" />
    <env name="APP_DEBUG" value="0" />
    <env name="SHELL_VERBOSITY" value="-1" />
    <!-- define your env variables for the test env here -->
</php>

<testsuites>
    <testsuite name="Plein Test Suite">
        <directory>tests/unit</directory>
    </testsuite>
</testsuites>

我查看了所有相关问题,但找不到好的答案,请问有什么想法吗?

symfony phpunit symfony4
2个回答
23
投票

我通过将以下内容添加到我的

phpunit.xml.dist
/
phpunit.xml
文件(在
phpunit
标签内)解决了这个问题:

<php>
    <env name="APP_ENV" value="test" force="true"/>
</php>

0
投票

对于我在

phpunit.xml.dist
中的 Symfony 6.4 来说,我必须改变

    <php>
        <server name="APP_ENV" value="test" force="true"/>
    </php>

    <php>
        <env name="APP_ENV" value="test" force="true"/>
    </php>

请注意标签

server
应更改为
env

© www.soinside.com 2019 - 2024. All rights reserved.