【问题标题】:Example PHPUnit Test on Laravel 5.4 fails with 404Laravel 5.4 上的示例 PHPUnit 测试失败并显示 404
【发布时间】:2017-06-30 13:51:55
【问题描述】:

我可以通过以下链接在 Laravel 5.4 上进行我的项目:localhost:8888/streaming_statistic/public

我有一个测试:

<?php

namespace Tests\Feature;

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

class ApiTest extends TestCase
{
    public function testBasicTest()
    {
        $response = $this->get('/');

        $response->assertStatus(200);
    }
}

我使用命令运行所有测试

./vendor/bin/phpunit

但结果是:

PHPUnit 5.7.13 by Sebastian Bergmann and contributors.

F                                                                   1 / 1 (100%)

Time: 222 ms, Memory: 12.25MB

There was 1 failure:

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

/Applications/MAMP/htdocs/streaming_statistic/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestResponse.php:56
/Applications/MAMP/htdocs/streaming_statistic/tests/Feature/ApiTest.php:16

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.

我可以打开这个页面http://localhost:8888/streaming_statistic/public/ 我有一条路线:

Route::get('/', function () {
    return view('welcome');
});

我做错了什么? 在 laravel 中为控制器方法编写测试的正确方法是什么? HTTP 测试可能不是他们的最佳解决方案吗?

【问题讨论】:

  • 您需要为您的测试设置正确的 URL 和端口。
  • 我在哪里可以配置它们?
  • APP_URL=127.0.0.1:8888/streaming_statistic/public - 对于测试,这个 env 变量是错误的......我将根目录更改为 streaming_statistic/public 并将 env 更改为 APP_URL=127.0.0.1:8888 - 并得到了 OK(1 个测试,1 个断言)!
  • 您可以在 phpunit.xml 文件中编辑它以影响测试环境。

标签: php testing laravel-5 phpunit laravel-5.4


【解决方案1】:

为了测试这个环境变量是错误的......

APP_URL=http://127.0.0.1:8888/streaming_statistic/public

我将根 Web 服务器目录更改为 streaming_statistic/public 并将 env 更改为

APP_URL=http://127.0.0.1:8888

之后我得到了:

OK (1 test, 1 assertion)

【讨论】:

  • 您可能想要验证测试是否真正通过。可能是在 127.0.0.1:8888 上,它看到了所有目录的列表,但并没有真正到达您的应用程序。
【解决方案2】:

我在 5 小时内锁定了同样的错误,终于找到了解决方案,

中更改APP_URL=http://localhost

.env.testing

文件

查看下面关于.env.testing文件的链接

.env.testing

【讨论】:

    猜你喜欢
    • 2017-10-28
    • 2017-05-24
    • 1970-01-01
    • 2017-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-26
    • 2017-09-10
    相关资源
    最近更新 更多