Laravel: PHPUnit causing 404s for everything(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::$baseUrl
、Config->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 tohttp://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
require
s that includes smaller route files. Taylor Otwell has said previously that a routes file like this, but containingrequire_once
can cause these 404 errors. He suggests changing torequire
. Some developers report that this fixes it for them, but mine have just beenrequire
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 withinIlluminateFoundationTestingConcerns
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Laravel:PHPUnit 导致所有内容出现 404
- Laravel 仓库 2022-01-01
- 没有作曲家的 PSR4 自动加载 2022-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01