【问题标题】:Laravel PHPUnit returns 404 only in first requestLaravel PHPUnit 仅在第一个请求中返回 404
【发布时间】:2021-12-13 17:55:00
【问题描述】:

我在使用 Laravel 7 时遇到了这个问题。

当我创建一个PHPUnit TestCase时,第一个请求返回404,但是第二个是成功的。

    $res = $this->json('post', '/api/auth/login', [
        'email' => $this->email,
        'password' => $this->password
    ]);

    Log::debug("---------------------");
    Log::debug("Response");
    Log::debug("---------------------");
    Log::debug(json_encode($res));
    Log::debug("---------------------");

    $response2 = $this->json('post', '/api/auth/login', [
        'email' => $this->email,
        'password' => $this->password
    ]);

    Log::debug("---------------------");
    Log::debug("Response2");
    Log::debug("---------------------");
    Log::debug(json_encode($response2));
    Log::debug("---------------------");

输出打印

"baseResponse":{"headers":{},"original":{"message":"","exception":"Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException"

我一直在阅读所有问题,但没有任何效果。

提前谢谢你!

【问题讨论】:

  • 第二次调用:Response2 工作正常

标签: laravel phpunit


【解决方案1】:

当我在类 RouteCollection 中设置日志时检测到错误

public function match(Request $request)
{

    Log::debug('---------------------');
    Log::debug('The request');
    Log::debug('---------------------');
    Log::debug($request->getMethod());
    Log::debug('---------------------');
    Log::debug($request->fullUrl());
    Log::debug('---------------------');
    $routes = $this->get($request->getMethod());

    // First, we will see if we can find a matching route for this current request
    // method. If we can, great, we can just return it so that it can be called
    // by the consumer. Otherwise we will check for routes with another verb.
    $route = $this->matchAgainstRoutes($routes, $request);

    return $this->handleMatchedRoute($request, $route);
}

第一个请求的结果是:

[2021-10-28 21:55:54] testing.DEBUG: ---------------------  
[2021-10-28 21:55:54] testing.DEBUG: The request  
[2021-10-28 21:55:54] testing.DEBUG: ---------------------  
[2021-10-28 21:55:54] testing.DEBUG: POST  
[2021-10-28 21:55:54] testing.DEBUG: ---------------------  
[2021-10-28 21:55:54] testing.DEBUG: http://127.0.0.1/abcapp/api/auth/login  
[2021-10-28 21:55:54] testing.DEBUG: ---------------------  

第二次的结果是:

[2021-10-28 21:55:54] testing.DEBUG: ---------------------  
[2021-10-28 21:55:54] testing.DEBUG: The request  
[2021-10-28 21:55:54] testing.DEBUG: ---------------------  
[2021-10-28 21:55:54] testing.DEBUG: POST  
[2021-10-28 21:55:54] testing.DEBUG: ---------------------  
[2021-10-28 21:55:54] testing.DEBUG: http://127.0.0.1/api/auth/login  
[2021-10-28 21:55:54] testing.DEBUG: ---------------------  

我的环境文件是:

APP_NAME=ABCApp
APP_ENV=local
APP_KEY=base64:HelloGuys!
APP_DEBUG=true
APP_URL=http://localhost/abcapp/
LOG_CHANNEL=daily

我的 env.testing 文件是:

APP_NAME=ABCApp
APP_ENV=local
APP_KEY=base64:HelloGuys!
APP_DEBUG=true
APP_URL=http://localhost/abcapp/
LOG_CHANNEL=daily

我解决了从 env.testing 更改 APP_URL 的错误:

APP_NAME=ABCApp
APP_ENV=local
APP_KEY=base64:HelloGuys!
APP_DEBUG=true
APP_URL=http://127.0.0.1/
LOG_CHANNEL=daily

之后我清除缓存。然后工作!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-03
    • 2020-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-10
    • 2016-07-19
    • 1970-01-01
    相关资源
    最近更新 更多