【问题标题】:error 404 on example test with Laravel 5.4使用 Laravel 5.4 进行示例测试时出现错误 404
【发布时间】:2017-10-28 21:41:11
【问题描述】:

使用 Laravel 5.4,我无法运行为 phpunit 提供的示例测试:

<?php

namespace Tests\Feature;

use Tests\TestCase;
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 testBasicTest()
    {
        $response = $this->get('/');

        $response->assertStatus(200);
    }
}

从我的项目输出的根目录运行命令phpunit

PHPUnit 6.0.10 by Sebastian Bergmann and contributors.

F.                                                                  2 / 2 (100%)

Time: 147 ms, Memory: 12.00MB

There was 1 failure:

1) Tests\Feature\ExampleTest::testBasicTest
Expected status code 200 but received 404.
Failed asserting that false is true.

/home/username/public_html/myproject/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestResponse.php:76
/home/username/public_html/myproject/tests/Feature/ExampleTest.php:21

我的.env 在根目录下包含:

APP_URL=http://localhost/~username/myproject/public

我看到了this similar question,但是回答没用。

有什么想法吗?

编辑:

请注意,网址 http://localhost/~username/myproject/public/ 可以通过浏览器完美运行。

【问题讨论】:

  • 您的网站可以通过浏览器正常运行吗?
  • @NigelRen 是的,该站点的稳定版本正在运行。当前的开发版本为每个页面返回错误 500,但通过浏览器工作的根 url 除外。这就是我想运行测试的原因,但是这个对根 url 的请求的示例首先不起作用
  • 您能否将echo env('APP_URL', '?'); 添加到您的测试中以验证​​它具有正确的配置。
  • @NigelRen echo env('APP_URL', '?'); 返回了http://localhost/~username/myproject/public,所以这是正确的配置,不是吗?
  • 添加虚拟主机,并将该域用于 APP_URL

标签: php laravel phpunit laravel-5.4


【解决方案1】:

添加虚拟主机,并将该域用于APP_URL

在 Ubuntu 17.04 中,您可以通过在 /etc/apache2/sites-available 中创建具有以下内容的 .conf 文件来实现:

<VirtualHost *:80>
    DocumentRoot "/path/to/your/laravel/project/public"
    ServerName name.of.your.virtual.host

    <Directory "/path/to/your/laravel/project/public">
        AllowOverride All
    </Directory>
</VirtualHost>

然后用apache命令开启虚拟主机:

sudo a2ensite name.of.your.file.conf

最后重启apache:

systemctl reload apache2

【讨论】:

    猜你喜欢
    • 2017-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    • 2018-07-06
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多