Laravel:PHPUnit 导致所有内容出现 404 [英] Laravel: PHPUnit causing 404s for everything

查看:33
本文介绍了Laravel:PHPUnit 导致所有内容出现 404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 PHPUnit 为我的 Laravel 应用程序运行一些基本的单元测试.

I'm trying to run some basic unit tests for my Laravel application with PHPUnit.

但是,所有 HTTP 请求都返回 404 not found HTTP 状态码,所以我们在第一关就失败了.

However, all HTTP requests return a 404 not found HTTP status code, so we are failing at the first hurdle.

在网上搜索后,我找到了有关如何解决此问题的信息,但到目前为止,这些都没有真正起作用:

After trawling the internet, I have found information about how to remedy this, but so far none of these actually work:

  • 检查我是否正确引用了命名路由.
  • 检查此路由是否有控制器方法.
  • 检查它是否在浏览器中工作.
  • 我已确保我的应用程序 url 在所有正确的位置设置正确:TestCase::$baseUrlConfig->app->url...它设置为 http://localhost/myappfolder
  • 我在单元测试方法中尝试了不同的方法来获取 HTTP 响应对象.到目前为止,所有结果都是 404:$this->route()$this->action()$this->call().
  • 我已尝试从我的控制器中删除对路由保护中间件的所有引用.我相信它在运行单元测试时被禁用,但我已经这样做了,因为我正在努力诊断这个问题.没有效果.
  • 我的应用程序的根文件有几个 PHP require,其中包括较小的路由文件.Taylor Otwell 之前说过,像这样的路由文件,但包含 require_once 可能会导致这些 404 错误.他建议改为require.一些开发人员报告说这为他们解决了问题,但我的一直是require.无论如何,我也尝试将路线移回主路线文件,但这也没有奏效.
  • Checking I've referenced the named route correctly.
  • Checking that this route has a controller method.
  • Checking that it works in the browser.
  • I've ensured that my application url is set correctly in all the right places: TestCase::$baseUrl, Config->app->url...Its set to http://localhost/myappfolder
  • I've tried different methods from within my unit test methods to get an HTTP response object. So far all result in a 404: $this->route(), $this->action(), $this->call().
  • I've tried removing all references to route protection Middleware from my controllers. I believe that it is disabled when running unit tests, but I've done this as I'm pulling my proverbial hair out trying to diagnose this problem. No effect.
  • My application's roots file has several PHP requires that includes smaller route files. Taylor Otwell has said previously that a routes file like this, but containing require_once can cause these 404 errors. He suggests changing to require. Some developers report that this fixes it for them, but mine have just been require all along. I have also tried moving routes back into the main routes file anyway, but this has not worked either.

调查通常不寻常:

  • 如果我在 IlluminateFoundationTestingConcerns 中编辑 route() 方法以打印出它生成的 URL,它实际上是完美的:http://localhost/myprojectfolder/route-url-text.在浏览器中访问此页面工作正常.使用 cURL 访问它也可以.在生成的 URL 上运行 $this->call(),然后运行 ​​PHPUnit 再次显示 404.所以它只是 PHPUnit 做了一些奇怪的事情.
  • If I edit the route() method within IlluminateFoundationTestingConcerns to print out the URL it generates, it is in fact perfectly formed: http://localhost/myprojectfolder/route-url-text. Visiting this page in the browser works fine. Visiting it with cURL works too. Running $this->call() on the URL thats been generated, and then running PHPUnit shows the 404 again. So its just PHPUnit doing something weird.

这是我在单元测试脚本中使用的代码:

Here is the code I'm using in the unit test script:

public function testThis()
{
    $response = $this->route('GET', 'myRouteName', [
        'myParam' => 5
    ]);
    dd($response->status()); // 404 - always!
}

好像有什么东西介入并明确抛出 404...如果有人能阐明这一点,我将不胜感激.

Its like there is something stepping in and explicitly throwing a 404...Would really appreciate if someone could shed light on this.

推荐答案

问题解决了.我在 app/Providers/AppServiceProvider.php 中有这个.添加到集体 phpunit 故障排除指南的内容!

Problem solved. I had this inside app/Providers/AppServiceProvider.php. Something to add to the collective phpunit troubleshooting guide!

public function boot()
{
    URL::forceRootUrl( Config::get('app.url') );
}

我已将其更改为:

public function boot()
{
    if (!defined('PHPUNIT_RUNNING')) {
        URL::forceRootUrl( Config::get('app.url') );
    }
}

并把这个放在 phpunit.xml

<php>
    <const name="PHPUNIT_RUNNING" value="true"/>
</php>

这篇关于Laravel:PHPUnit 导致所有内容出现 404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆