Laravel phpunit 返回 404

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47381597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:58:56  来源:igfitidea点击:

Laravel phpunit returns 404

laravelphpunit

提问by Илья Алисов

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

我是 Laravel 的新手。使用本地开发环境与 Homestead 尝试运行简单的测试

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 :

路线/ web.php :

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

Auth::routes();

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

running phpunit returns 404 error

运行 phpunit 返回 404 错误

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

vagrant@homestead:~/code/laravel$ phpunit PHPUnit 6.4.3 由 Sebastian Bergmann 和贡献者编写。

。F。3 / 3 (100%)

时间:1.07 秒,内存:10.00MB

有 1 次失败:

1) Tests\Feature\UserTest::testBasicTest 预期状态代码 200 但收到 404。断言 false 为真失败。

/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 404updateTestCase.php

1. Laravel phpunit 总是 404updateTestCase.php

    protected $baseUrl = 'http://localhost'

change with

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

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

2. https://github.com/dingo/api/issues/540和旁边的 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
    
  1. 将 .env 中的 APP_URL 更改为

    APP_URL=laravel.local
    

none of this helped

这些都没有帮助

'laravel.local' works fine in browser

'laravel.local' 在浏览器中工作正常

Please, help me fix it

请帮我修复它

回答by Mihir Kagrana

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

我在浏览器中使用以下 URL 运行我的应用程序。 http://localhost/app/public/

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

具有以下行的默认单元测试失败。它得到 404 响应而不是 200。

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

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

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

阅读上述评论后,我将 .env 文件中的 APP_URL 更改为如下所示。

APP_URL=http://localhost

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.

但我仍然收到 404 错误。经过一些试验和错误,我发现我需要清除配置缓存。

php artisan config:cache

php artisan config:cache

This did the trick and my test started working!!

这成功了,我的测试开始工作了!!

回答by Daniel Roman

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

我有同样的问题,如果我用 php artisan serve 运行 laravel,它必须是:

APP_URL=http://127.0.0.1:8000/

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

APP_URL,必须是完全网站的URL。

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

该网站正在运行,但我没有正确设置该常量,并且 PHPUnit 一直在说 404。

回答by whiterook6

Have you tried with process isolationset to truein your phpunit.xmlfile? Our functional tests fail with 404 errors all over the place if we do not have process isolation:

你试过在你的文件中process isolation设置为吗?如果我们没有进程隔离,我们的功能测试就会失败,到处都是 404 错误:truephpunit.xml

<phpunit backupGlobals="false"
 backupStaticAttributes="false"
 bootstrap="bootstrap/autoload.php"
 colors="true"
 convertErrorsToExceptions="true"
 convertNoticesToExceptions="true"
 convertWarningsToExceptions="true"
 processIsolation="true"
 stopOnFailure="false">

回答by TooCooL

For anyone having this problem, use APP_URL=http://localhost, this happens if you use vagrant I am not sure for other envs, still investigating.

对于遇到此问题的任何人,请使用APP_URL=http://localhost,如果您使用 vagrant 会发生这种情况我不确定其他环境,仍在调查中。

回答by medard mandane

The solution is to simply test using the url returned by

解决方案是简单地使用返回的 url 进行测试

php artisan serve

as base url then run the tests

作为基本网址然后运行测试

For example:

例如:

    $response = $this->post('http://127.0.0.1:8001/api/register', [
        'email' => '',
        'phone' => '',
        'password' => '',
        'c_password' => '',
        'role_id' => '',
    ]);

    $response->assertStatus(401);

回答by frank_108

As @TooCooL mentioned, one could set APP_URL=http://localhosteven if your project might be something as APP_URL=http://localhost/myproject.
Even better is to create another .env, it shall be called env.testingand there one can change APP_URL or even another database for testing purposes only and so on.
Phpunit will look for configuration at that file .env.testing
Although phpunit works fine even when APP_URL is set on my vhost name on my lamp stack.
Check out your /etc/hosts file as well.

正如@TooCooL 所提到的,APP_URL=http://localhost即使您的项目可能是APP_URL=http://localhost/myproject.
更好的是创建另一个.env,它应该被调用,env.testing并且可以更改 APP_URL 甚至另一个数据库,仅用于测试目的等等。
Phpunit 将在该文件中查找配置,.env.testing
尽管 phpunit 工作正常,即使在我的灯堆栈上的虚拟主机名称上设置了 APP_URL。
还要检查您的 /etc/hosts 文件。