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

时间:2019-05-22 10:33:36

标签: php laravel laravel-5 phpunit homestead

您能帮我在本地的宅基地/无用机器上解决此问题吗?

当我运行命令时:PHPUnit

我得到这个结果

  

Sebastian Bergmann和贡献者的PHPUnit 4.8.36。

     

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

                                                                                                               There was 1 error:                                                    

                                                                                                               1) ExampleTest::testBasicExample                                      
     

无法修改标头信息-标头已由发送(输出   从/ 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

请帮助!我已经尝试了一切!

TestCase文件

<?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;
    }
}

ExampleTest文件:

<?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');
    }
}

3 个答案:

答案 0 :(得分:1)

我也遇到了这个问题。

我已经通过使用预防性的 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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

add stderr=true for phpunit.
<phpunit backupGlobals="false"
     backupStaticAttributes="false"
     bootstrap="vendor/autoload.php"
     colors="true"
     stderr="true"
     ...
 </phpunit>

或使用注解

/**
 * @runInSeparateProcess
 */

为我解决了。希望能帮到你。