Laravel PHPUnit返回404 [英] Laravel phpunit returns 404
问题描述
我是Laravel的新朋友.在Homestead中使用本地开发环境 尝试运行简单的测试
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:
routes/web.php :
Route::get('/', function () {
return view('main');
});
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
运行phpunit返回404错误
running phpunit returns 404 error
vagrant @ homestead:〜/code/laravel $ phpunit PHPUnit 6.4.3 by塞巴斯蒂安(Sebastian) 伯格曼和贡献者.
vagrant@homestead:~/code/laravel$ phpunit PHPUnit 6.4.3 by Sebastian Bergmann and contributors.
.F. 3 /3(100%)
.F. 3 / 3 (100%)
时间:1.07秒,内存:10.00MB
Time: 1.07 seconds, Memory: 10.00MB
有1个失败:
1)Tests \ Feature \ UserTest :: testBasicTest预期状态码为200,但 收到404.无法断言false为真.
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
/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始终为404 updateTestCase.php
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 及其旁边 https://github.com/dingo/api/issues/571
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;
-
将.env中的APP_URL更改为
Changing APP_URL in .env to
APP_URL=laravel.local
没有一个帮助
'laravel.local'在浏览器中正常工作
'laravel.local' works fine in browser
请帮我修复它
推荐答案
我正在浏览器中运行带有以下URL的应用程序. http://localhost/app/public/
I was running my application with following URL in browser. http://localhost/app/public/
具有下面一行的默认单元测试失败.它得到了404个响应而不是200个响应.
The default unit test having below line was failing. It was getting 404 response instead of 200.
$response = $this->get('/');
$response->assertStatus(200);
$response = $this->get('/');
$response->assertStatus(200);
在阅读上述评论后,我如下更改了.env文件中的APP_URL.
I changed APP_URL in .env file as below after reading above comments.
APP_URL=http://localhost
但是我仍然遇到404错误.经过一番尝试和错误后,我发现我需要清除配置缓存.
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!!
这篇关于Laravel PHPUnit返回404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!