[英]Laravel 5.2 - phpunit - Cannot modify header information - headers already sent by
Can you please help me solve this issue on my local homestead/vagrant machine?你能帮我在我本地的宅基地/流浪机器上解决这个问题吗?
When I run the command: PHPUnit当我运行命令时:PHPUnit
I getting this result我得到这个结果
PHPUnit 4.8.36 by Sebastian Bergmann and contributors.
PHPUnit 4.8.36 由 Sebastian Bergmann 和贡献者编写。
E
乙
Time: 1.97 seconds, Memory: 6.00MB时间:1.97 秒,内存: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/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/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/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
Please help!请帮忙! I've tried everything!!!
我什么都试过了!!!
TestCase File测试用例文件
<?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 File:示例测试文件:
<?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');
}
}
I have found the answer.我找到了答案。 In one of my file, I added Header functions.
在我的一个文件中,我添加了 Header 函数。 So when I removed them.
所以当我删除它们时。 everything worked fine :)
一切正常:)
I've also had this issue.我也遇到过这个问题。
I have solved this by just using preventive headers_sent function, so there was no need to delete the header() functions from my code:我已经通过使用预防性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');
}
For a small bulk of tests you can also use this: alternative solution对于少量测试,您也可以使用:替代解决方案
In this answer there is a full explanation about this situation在这个答案中有关于这种情况的完整解释
add stderr=true for phpunit.
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
stderr="true"
...
</phpunit>
or use annotations或使用注释
/**
* @runInSeparateProcess
*/
resolved for me.为我解决。 i hope will help you.
我希望会帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.