【问题标题】:Laravel Expected status code 200 but received 500Laravel 预期状态码 200 但收到 500
【发布时间】:2021-05-24 20:24:56
【问题描述】:

这是我的单元测试的代码,默认的:

<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ExampleTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testBasicTest()
    {
        $response = $this->get('/');

        $response->assertStatus(200);
    }
}

我正在运行 laravel 项目:php artisan serv

当我手动访问/ 时,项目运行良好!但是在测试中我收到了这个错误:

  Expected status code 200 but received 500.
  Failed asserting that 200 is identical to 500.

这是完整的输出:

  FAIL  Tests\Feature\ExampleTest
  ⨯ basic test

  ---

  • Tests\Feature\ExampleTest > basic test
  Expected status code 200 but received 500.
  Failed asserting that 200 is identical to 500.

  at tests/Feature/ExampleTest.php:19
     15▕     public function testBasicTest()
     16▕     {
     17▕         $response = $this->get('/');
     18▕ 
  ➜  19▕         $response->assertStatus(200);
     20▕     }
     21▕ }
     22▕

请问我该如何解决?

【问题讨论】:

  • 测试运行输出中是否显示堆栈跟踪?这应该会为您提供有关 500 错误的一些信息,例如文件和行号。
  • @JamesClarkDeveloper 我确实放了完整的输出
  • 您的测试环境似乎有问题导致错误。测试运行的输出中可能包含更多信息,其中包括称为堆栈跟踪的内容。这应该包含有关遇到的 500 错误的更多信息,并帮助您指出问题所在。我建议花一些时间学习 php 中的堆栈跟踪(快速的 google 搜索会找到一些关于该主题的漂亮博客文章),看看这是否有助于你前进。祝你好运!

标签: php laravel laravel-5 phpunit tdd


【解决方案1】:

在您的$this-&gt;get() 呼叫之前,插入对$this-&gt;withoutExceptionHandling() 的呼叫。它将向您显示错误的详细信息。

【讨论】:

  • 结束会话权限被拒绝。
猜你喜欢
  • 2019-03-20
  • 2018-11-26
  • 2018-03-01
  • 2016-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-01
相关资源
最近更新 更多