【问题标题】:Laravel 5.4 - Basic Tests - NotFoundHttpExceptionLaravel 5.4 - 基本测试 - NotFoundHttpException
【发布时间】:2017-11-23 19:08:03
【问题描述】:

我在运行 Laravel 附带的基本测试时遇到问题:

app/tests/Feature/ExampleTest.php

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

        $response->assertStatus(200);
        $this->assertTrue(true);
    }
}

当我运行它时,我得到了异常NotFoundHttpException。我可以毫无问题地在浏览器中访问我的网站。这个问题似乎适用于我的所有路线。

使用 Laravel 5.4

路由/app/routes/web.php中定义。

【问题讨论】:

  • 你能把routes.php的代码贴出来吗?
  • app/routes/web.php<?php Route::get('/', function () { return view('welcome'); });
  • 所以你想访问 testBasicTest() 函数但不返回视图,对吧?
  • 我刚刚发现了一些非常有趣的东西。我的机器托管多个网页。在我的 Laravel .env 文件中,我有:APP_URL=https://myserver/product-service,将其更改为 APP_URL=https://myserver 可以防止 NotFoundHttpException。然而,这不是解决方案
  • 似乎与这个问题有关:stackoverflow.com/questions/22967227/…

标签: php laravel unit-testing routes phpunit


【解决方案1】:

似乎在 Laravel 5.4 中 $baseUrl 属性已从 TestCase 类中删除。

您可以在您的ExampleTest 中添加一些setUp

function setUp()
{
    parent::setUp();
    config(['app.url' => 'https://myserver']);
}

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2017-08-21
    • 2018-02-19
    • 2014-02-10
    • 1970-01-01
    • 2018-02-13
    • 2019-01-13
    • 2017-11-12
    • 2015-04-13
    • 2018-03-03
    相关资源
    最近更新 更多