Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel phpunit returns 404

i am new in Laravel. Using local development environment with Homestead Try to run simple test

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

class UserTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */

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

        $response->assertStatus(200);
    }
}

routes/web.php :

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

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

running phpunit returns 404 error

vagrant@homestead:~/code/laravel$ phpunit PHPUnit 6.4.3 by Sebastian Bergmann and contributors.

.F. 3 / 3 (100%)

Time: 1.07 seconds, Memory: 10.00MB

There was 1 failure:

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

/home/vagrant/code/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestResponse.php:78 /home/vagrant/code/laravel/tests/Feature/UserTest.php:25

I have tried to fix it with these ways

1. Laravel phpunit always 404 updateTestCase.php

    protected $baseUrl = 'http://localhost'

change with

    protected $baseUrl = 'http://laravel.local'

2. https://github.com/dingo/api/issues/540 and next to it https://github.com/dingo/api/issues/571

    use Illuminate\Foundation\Testing\WithoutMiddleware;
    class BankTest extends TestCase {
        use WithoutMiddleware;
  1. Changing APP_URL in .env to

    APP_URL=laravel.local
    

none of this helped

'laravel.local' works fine in browser

Please, help me fix it

like image 677
Илья Алисов Avatar asked Nov 19 '17 20:11

Илья Алисов


People also ask

How do I fix laravel 404 error?

By adding the . htaccess file at the same folder location of the index. php it solves the 404 page not found error in my laravel Project.

How do I change to 404 in laravel?

You need to create blade views for error pages, move to this path resources/views/ inside here create errors folder and within the directory create 404. blade. php file. It will redirect you to the 404 page if you don't find the associated URL.

What is PHPUnit laravel?

Use Laravel Unit Testing to Avoid Project-Wrecking Mistakes. Pardeep Kumar. 7 Min Read. PHPUnit is one of the most well known and highly optimized unit testing packages of PHP. It is a top choice of many developers for rectifying different developmental loopholes of the application.


2 Answers

I was running my application with following URL in browser. http://localhost/app/public/

The default unit test having below line was failing. It was getting 404 response instead of 200.

$response = $this->get('/'); $response->assertStatus(200);

I changed APP_URL in .env file as below after reading above comments.

APP_URL=http://localhost

But I was still getting 404 error. After some trial and errors, I found that I needed to clear config cache.

php artisan config:cache

This did the trick and my test started working!!

like image 73
Mihir Kagrana Avatar answered Oct 04 '22 01:10

Mihir Kagrana


I had the same problem, and if I run laravel with php artisan serve, it has to be:

APP_URL=http://127.0.0.1:8000/

The APP_URL, has to be exactly the URL of the website.

The website was working, but I didn't have that constant correctly, and PHPUnit was saying all time 404.

Don't forget to clear the cache, after change the config

php artisan config:cache
like image 8
Dani Roman Avatar answered Oct 03 '22 01:10

Dani Roman