lumen unit test GET '/' Expected status code 200, got 500.

解决方法: https://blog.csdn.net/fareast_mzh/article/details/82179781

.\vendor\bin\phpunit
PHPUnit 7.2.7 by Sebastian Bergmann and contributors.

F...                                                                4 / 4 (100%)

Time: 462 ms, Memory: 8.00MB

There was 1 failure:

1) ExampleTest::testExample
Expected status code 200, got 500.
Failed asserting that false is true.

E:\easyeye\src\admin\vendor\laravel\lumen-framework\src\Testing\Concerns\MakesHttpRequests.php:397
E:\easyeye\src\admin\tests\ExampleTest.php:20
E:\easyeye\src\admin\tests\ExampleTest.php:25
 

* call stack

.\vendor\phpunit\phpunit\src\Framework\TestCase.php

.\vendor\laravel\lumen-framework\src\Testing\TestCase.php

.\tests\TestCase.php

.\tests\ExampleTest.php

 

* test case

.\tests\ExampleTest.php

<?php
declare(strict_types=1);

use Laravel\Lumen\Testing\DatabaseMigrations;
use Laravel\Lumen\Testing\DatabaseTransactions;

class ExampleTest extends TestCase {
    protected $baseUrl = 'http://127.0.0.1:8000';

    /**
     * A basic test example.
     *
     * @return void
     */
    public function testExample() {
        $this->get('/');

        // var_dump($this->response); die;

        $this->assertResponseOk();
        $this->assertEquals($this->app->version(), $this->response->getContent());
    }

    public function run(\PHPUnit\Framework\TestResult $result = null): \PHPUnit\Framework\TestResult {
        return parent::run($result);
    }
}

var_dump($this->response);

E:\easyeye\src\admin\tests\ExampleTest.php:18:
class Illuminate\Http\Response#338 (8) {
  public $headers =>
  class Symfony\Component\HttpFoundation\ResponseHeaderBag#337 (5) {
    protected $computedCacheControl =>
    array(2) {
      'no-cache' =>
      bool(true)
      'private' =>
      bool(true)
    }
    protected $cookies =>
    array(0) {
    }
    protected $headerNames =>
    array(2) {
      'cache-control' =>
      string(13) "Cache-Control"
      'date' =>
      string(4) "Date"
    }
    protected $headers =>
    array(2) {
      'cache-control' =>
      array(1) {
        ...
      }
      'date' =>
      array(1) {
        ...
      }
    }
    protected $cacheControl =>
    array(0) {
    }
  }
  protected $content =>
  string(36016) "<!DOCTYPE html>
<html>
    <head>
        <meta name="robots" content="noindex,nofollow" />
        <style>
            /* Copyright (c) 2010, Yahoo! Inc. All rights reserved. Code licensed under the BSD License: http://developer.yahoo.com/yui/license.html */
            html{color:#000;background:#FFF;}body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,textarea,p,blockquote,th,td{margin:0;padding:0;}table{border-collapse:collapse;border-spacing:0;}fieldset,img{border:0;}"...
  protected $version =>
  string(3) "1.0"
  protected $statusCode =>
  int(500)
  protected $statusText =>
  string(21) "Internal Server Error"
  protected $charset =>
  NULL
  public $original =>
  string(36016) "<!DOCTYPE html>
<html>
    <head>
        <meta name="robots" content="noindex,nofollow" />
        <style>
            /* Copyright (c) 2010, Yahoo! Inc. All rights reserved. Code licensed under the BSD License: http://developer.yahoo.com/yui/license.html */
            html{color:#000;background:#FFF;}body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,textarea,p,blockquote,th,td{margin:0;padding:0;}table{border-collapse:collapse;border-spacing:0;}fieldset,img{border:0;}"...
  public $exception =>
  class ErrorException#343 (9) {
    protected $message =>
    string(147) "Cannot modify header information - headers already sent by (output started at E:\easyeye\src\admin\vendor\phpunit\phpunit\src\Util\Printer.php:109)"
    private $string =>
    string(3425) "ErrorException: Cannot modify header information - headers already sent by (output started at E:\easyeye\src\admin\vendor\phpunit\phpunit\src\Util\Printer.php:109) in E:\easyeye\src\admin\app\Http\Middleware\CorsMiddleware.php:51
Stack trace:
#0 [internal function]: Laravel\Lumen\Application->Laravel\Lumen\Concerns\{closure}(2, 'Cannot modify h...', 'E:\\easyeye\\src\\...', 51, Array)
#1 E:\easyeye\src\admin\app\Http\Middleware\CorsMiddleware.php(51): header('Access-Control-...')
#2 E:\easyeye\src\admin\ven"...
    protected $code =>
    int(0)
    protected $file =>
    string(59) "E:\easyeye\src\admin\app\Http\Middleware\CorsMiddleware.php"
    protected $line =>
    int(51)
    private $trace =>
    array(23) {
      [0] =>
      array(4) {
        ...
      }
      ...
    }
    private $previous =>
    NULL
    protected $severity =>
    int(2)
    public $xdebug_message =>
    string(3516) "
ErrorException: Cannot modify header information - headers already sent by (output started at E:\easyeye\src\admin\vendor\phpunit\phpunit\src\Util\Printer.php:109) in E:\easyeye\src\admin\app\Http\Middleware\CorsMiddleware.php on line 51

Call Stack:
    0.0008     381120   1. {main}() E:\easyeye\src\admin\vendor\phpunit\phpunit\phpunit:0
    0.0217    1356656   2. PHPUnit\TextUI\Command::main() E:\easyeye\src\admin\vendor\phpunit\phpunit\phpunit:53
    0.0218    1361216   3. PHPUnit\TextUI\Command->run() "...
  }
}

.\vendor\phpunit\phpunit\src\Util\Printer.php:109

CORS

* .\bootstrap\app.php 先禁用

$app->middleware([
    // App\Http\Middleware\CorsMiddleware::class,
    Illuminate\Session\Middleware\StartSession::class
]);

$app->routeMiddleware([
    'auth' => App\Http\Middleware\Authenticate::class,
    // 'cors' => App\Http\Middleware\CorsMiddleware::class
]);

workaround:

header之前不能有任何输出

error_reporting(E_ERROR | E_PARSE);

Solution:

换一个cors

https://packagist.org/packages/barryvdh/laravel-cors

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fareast_mzh

打赏个金币

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值