dqq9695 2016-03-25 22:55
浏览 50
已采纳

无法使用PHPUnit对vagrant box执行任何测试

I am working for a company. I know how to ran PHPUnit tests on vagrant box. But I don't know why i can't run any test on their vagrant box configured using chef. Whenever i am running PHPUnit command, i am getting below error:

There was 1 failure:

1) ExampleTest::testBasicExample
A request to [en/contact] failed. Received status code [404].

/..../vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:178
/..../vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:72
/..../vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:53
/..../tests/ExampleTest.php:16

Caused by
ErrorException: Cannot modify header information - headers already sent by (output started at /..../vendor/phpunit/phpunit/src/Util/Printer.php:133) in /..../app/Http/Middleware/SetCookieData.php:17

Any help will be very much appreciable.

  • 写回答

1条回答 默认 最新

  • dongzhonggua4229 2016-03-26 06:45
    关注

    Your test is running just fine. In your example test, you made a call to visit('en/contact'). It attempted to visit that url, but instead of a 200, it got a 404. The display you are seeing is the result of your test attempting to visit that url.

    The problem is that your code in app/Http/Middleware/SetCookieData.php is attempting to modify the headers (using the header() function), but PHPUnit has already written output to STDOUT. So, PHP is throwing an error that you're attempting to modify headers after writing output.

    There are a couple answers[1][2][3] for this issue already, but to summarize here:

    1. Run PHPUnit with the --stderr option (or add stderr="true" to your phpunit.xml). This way, PHPUnit will write to STDERR, so PHP won't complain about having already written to STDOUT. This is probably the easiest option.

    2. Trying using the @runInSeparateProcess annotation for your test. Though, you may run into issues because of the complexity of the framework. Additionally, you will probably have many tests that need this annotation, and that will be resource intensive.

      /**
       * @runInSeparateProcess
       */
      public function testBasicExample()
      {
          // visit('en/contact');
      }
      
    3. Run PHPUnit with the --process-isolation flag (or add processIsolation="true" to your phpunit.xml). Likely same issues as the @runInSeparateProcess annotation.

    4. Mock the built-in header() function to not actually send headers. Though, this may change how your pages react, so you will probably end up having to write more testing code.

    References:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 调用函数时,无关变量的改变引起函数值的改变
  • ¥15 xy坐标转化为经纬度坐标
  • ¥15 一般三角模糊数的上界值和下届值取中值的多少比较合理?
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥20 Hbase启动失败,无法启动HMaster
  • ¥15 sqpingboot在父模块已经引入了mybatisplus 在子组件不写版本号还是报错
  • ¥20 Lumerical FDTD solutions 中模型的相对阻抗,有效介电常数和有效磁导率的实部和虚部的数据如何获得?
  • ¥100 sql reporting service 远程smtp服务器配置支持
  • ¥15 ppyoloe_r带角度目标检测,loss_cls没法收敛
  • ¥15 淘宝交易指数如何解读,其关联的数据指标是什么