cakephp php单元测试单控制器正常,但在完整测试中失败

h22fl7wq  于 2022-11-11  发布在  PHP
关注(0)|答案(1)|浏览(165)

对于cakephp 4.4.2,我有以下测试:

public function testVindex(): void
{
    $method = '/historiques/vindex';

    // Not connected

    $this->get($method);
    $this->assertRedirect(['controller' => 'Users', 'action' => 'login']);

}

当我使用以下组件测试此控制器时,它工作正常:

vendor/bin/phpunit  tests/TestCase/Controller/HistoriquesControllerTest.php

但是,当使用其他设备完成该测试时,使用:

vendor/bin/phpunit

这将生成一个错误:

There was 1 failure:

1) App\Test\TestCase\Controller\HistoriquesControllerTest::testVindex
Possibly related to Authentication\Authenticator\UnauthenticatedException: "No identity found." 
...
Failed asserting that response has header 'Location'.

/opt/applications2/p35/vendor/cakephp/cakephp/src/TestSuite/IntegrationTestTrait.php:839
/opt/applications2/p35/tests/TestCase/Controller/HistoriquesControllerTest.php:53

--

你知道吗?
谢谢

erhoui1w

erhoui1w1#

我发现了问题,另一个测试有副作用。
另一个测试需要模拟一个 AJAX 请求,以便做到这一点:

$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'; 

$this->get('/contests/synthese');
$this->assertResponseOk();
$this->assertHeader('Content-Type', 'application/json');
$this->assertContentType('application/json');

要纠正这个问题,我只需要:

$_SERVER['HTTP_X_REQUESTED_WITH'] = ''

在测试结束时

相关问题