Example PHPUnit Test on Laravel 5.4 fails with 404

2019-06-17 00:22发布

I can my project on Laravel 5.4 from link: localhost:8888/streaming_statistic/public

I have a test:

<?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);
    }
}

I run all tests with command

./vendor/bin/phpunit

But result is:

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.

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

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

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?

2条回答
叛逆
2楼-- · 2019-06-17 00:41

I have Locked the same error on 5 hours, Finally, I got a solution,

Change the APP_URL=http://localhost in

.env.testing

file

Check the below link about .env.testing file

.env.testing

查看更多
男人必须洒脱
3楼-- · 2019-06-17 00:57

for tests this env variable was wrong...

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

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

APP_URL=http://127.0.0.1:8888

After that i got:

OK (1 test, 1 assertion)
查看更多
登录 后发表回答