Laravel 5.2 - phpunit - 无法修改标头信息 - 标头已发送

Dan*_*nai 1 php phpunit laravel laravel-5 homestead

你能帮我在我本地的宅基地/流浪机器上解决这个问题吗?

当我运行命令时:PHPUnit

我得到这个结果

PHPUnit 4.8.36 由 Sebastian Bergmann 和贡献者编写。

E
时间:1.97 秒,内存:6.00MB

                                                                                                               There was 1 error:                                                    

                                                                                                               1) ExampleTest::testBasicExample                                      
Run Code Online (Sandbox Code Playgroud)

无法修改标头信息 - 标头已发送(输出开始于 /home/vagrant/Code/Project/vendor/php unit/phpunit/src/Util/Printer.php:134)

                                                                                                               /home/vagrant/Code/Project/bootstrap/app.php:4                         
Run Code Online (Sandbox Code Playgroud)

/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;
    }
}
Run Code Online (Sandbox Code Playgroud)

示例测试文件:

<?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');
    }
}
Run Code Online (Sandbox Code Playgroud)

Dan*_*nai 5

我找到了答案。在我的一个文件中,我添加了 Header 函数。所以当我删除它们时。一切正常:)

  • 这不是一个解决方案:) (3认同)

Ale*_*mov 5

我也遇到过这个问题。

我通过使用预防性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');
}
Run Code Online (Sandbox Code Playgroud)

对于少量测试,您还可以使用以下方法:替代解决方案

在这个答案中有关于这种情况的完整解释