使用Laravel 5.4进行示例测试时出现错误404

时间:2017-05-28 13:39:23

标签: php laravel phpunit laravel-5.4

使用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/完全可以通过浏览器完成。

1 个答案:

答案 0 :(得分:2)

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

在Ubuntu 17.04中,您可以在.conf中创建一个/etc/apache2/sites-available文件,其中包含以下内容:

<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