Laravel 5.4上的示例PHPUnit测试失败并显示404 [英] Example PHPUnit Test on Laravel 5.4 fails with 404

查看:275
本文介绍了Laravel 5.4上的示例PHPUnit测试失败并显示404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以从以下链接访问Laravel 5.4上的项目:本地主机:8888/streaming_statistic/public

I can my project on Laravel 5.4 from link: 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/ 我有一条路线:

I can open this page http://localhost:8888/streaming_statistic/public/ I have an route:

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

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

What i am doing wrong? And what the right way to write tests for controller methods in laravel? May be the HTTP tests it not the best solution for them?

推荐答案

对于测试,此环境变量是错误的...

for tests this env variable was wrong...

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

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

I changed root web server directory to streaming_statistic/public and env to

APP_URL=http://127.0.0.1:8888

那之后我得到了:

OK (1 test, 1 assertion)

这篇关于Laravel 5.4上的示例PHPUnit测试失败并显示404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆