gpt4 book ai didi

php - Laravel 5.2 - phpunit - 无法修改 header 信息 - header 已发送

转载 作者:行者123 更新时间:2023-12-05 08:26:14 25 4
gpt4 key购买 nike

你能帮我在我本地的 homestead/vagrant 机器上解决这个问题吗?

当我运行命令时:PHPUnit

我得到这个结果

PHPUnit 4.8.36 by Sebastian Bergmann and contributors.

E
Time: 1.97 seconds, Memory: 6.00MB

                                                                                                               There was 1 error:                                                    

1) ExampleTest::testBasicExample

Cannot modify header information - headers already sent by (output started at /home/vagrant/Code/Project/vendor/php unit/phpunit/src/Util/Printer.php:134)

                                                                                                               /home/vagrant/Code/Project/bootstrap/app.php:4                         

/home/vagrant/Code/Poptin/tests/TestCase.php:20
/home/vagrant/Code/Project/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:85 /home/vagrant/Code/Project/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:64

求助!我什么都试过了!!!

测试用例文件

<?php

class TestCase extends Illuminate\Foundation\Testing\TestCase
{
/**
* The base URL to use while testing the application.
*
* @var string
*/
//protected $baseUrl = 'http://localhost';
protected $baseUrl = 'http://192.168.10.10';

/**
* Creates the application.
*
* @return \Illuminate\Foundation\Application
*/
public function createApplication()
{
$app = require __DIR__.'/../bootstrap/app.php';

$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();

return $app;
}
}

示例测试文件:

<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class ExampleTest extends TestCase
{
/**
* A basic functional test example.
*
* @return void
*/
public function testBasicExample()
{

$this->visit('/login')
->see('password');
}
}

最佳答案

我也遇到过这个问题。

我已经通过使用预防性的 headers_sent 函数解决了这个问题,所以没有必要从我的代码中删除 header() 函数:

if(!headers_sent()) {

header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization, X-Request-With, GoogleDataStudio');
header('Access-Control-Allow-Credentials: true');
}

对于少量测试,您还可以使用此:alternative solution

In this answer there is a full explanation about this situation

关于php - Laravel 5.2 - phpunit - 无法修改 header 信息 - header 已发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56254838/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com