【问题标题】:Laravel 5.2 Unit Test 404Laravel 5.2 单元测试 404
【发布时间】:2016-08-14 12:17:23
【问题描述】:

我设置了这个简单的测试,并试图找出为什么在运行此测试时会收到此错误消息。当我在浏览器中访问 myapp.app 时,我看到了安装 Laravel 5.2 的欢迎页面。

There were 2 failures:

1) ExampleTest::testExample
A request to [myapp.app] failed. Received status code [404].

家长测试类

<?php

use Illuminate\Foundation\Testing\DatabaseTransactions;

abstract class TestCase extends Illuminate\Foundation\Testing\TestCase
{
    use DatabaseTransactions;

    /**
     * The base URL to use while testing the application.
     *
     * @var string
     */
    protected $baseUrl = 'myapp.app';

    /**
     * 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;
    }
}

来自 Laravel 安装的默认示例测试

<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class ExampleTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testExample()
    {
        $this->visit('/')
            ->see('Laravel 5');
    }
}

【问题讨论】:

  • 查看post
  • 我没有使用 API 中间件

标签: laravel unit-testing testing laravel-5 laravel-5.2


【解决方案1】:

一个朋友告诉我的问题是,使用 xdebug 我可以输入代码并找出发生了什么。

之所以会这样,是因为在testcase文件中需要正确设置baseURL。

protected $baseUrl = 'http://myapp.app';

【讨论】:

    【解决方案2】:

    我设法通过将.env 文件的APP_URL 变量恢复为原始值来解决这个问题:

    APP_URL=http://localhost
    

    【讨论】:

      【解决方案3】:

      大多数时候,我使用“http://localhost”。你试过吗?

      【讨论】:

      • 我后来发现了问题所在,但你很接近。
      • 所以,@user3732216 添加答案以帮助其他人 ;)
      • 我在 26 分钟前做过。
      【解决方案4】:

      我遇到了类似的问题,但我的问题是我连接了额外的东西。 api 模块到主 api.php,像这样:

      Route::get(//some routes)
      ...
      
      require_once('entity.php');
      

      请不要这样做

      【讨论】:

        猜你喜欢
        • 2016-05-07
        • 2017-04-08
        • 1970-01-01
        • 2016-09-02
        • 2019-08-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-13
        • 2016-08-26
        相关资源
        最近更新 更多