[英]Laravel: PHPUnit causing 404s for everything
我正在尝试使用PHPUnit为我的Laravel应用程序运行一些基本的单元测试。
但是,所有HTTP请求都返回404未找到的HTTP状态代码,因此我们在第一个障碍时失败。
在浏览互联网之后,我找到了有关如何解决此问题的信息,但到目前为止,这些都没有实际工作:
TestCase::$baseUrl
, Config->app->url
...它设置为http://localhost/myappfolder
$this->route()
, $this->action()
, $this->call()
。 require
。 Taylor Otwell之前曾说过像这样的路由文件,但包含require_once
会导致这些404错误。 他建议改变require
。 一些开发人员报告说,这为他们修复了它,但我的一直都是require
。 我还尝试将路线移回主路线文件,但这也没有用。 调查通常是不寻常的:
Illuminate\\Foundation\\Testing\\Concerns
编辑route()
方法以打印出它生成的URL,它实际上是完美形成的: http://localhost/myprojectfolder/route-url-text
。 在浏览器中访问此页面工作正常。 使用cURL访问它也有效。 在生成的URL上运行$this->call()
,然后运行PHPUnit再次显示404。 所以它只是PHPUnit做一些奇怪的事情。 这是我在单元测试脚本中使用的代码:
public function testThis()
{
$response = $this->route('GET', 'myRouteName', [
'myParam' => 5
]);
dd($response->status()); // 404 - always!
}
就像有一些东西踩到并明确地抛出一个404 ......如果有人能够阐明这一点,我真的很感激。
问题解决了。 我在app/Providers/AppServiceProvider.php
里面有这个。 添加到集体phpunit故障排除指南的东西!
public function boot()
{
URL::forceRootUrl( Config::get('app.url') );
}
我已将此更改为:
public function boot()
{
if (!defined('PHPUNIT_RUNNING')) {
URL::forceRootUrl( Config::get('app.url') );
}
}
并把这个insun phpunit.xml
<php>
<const name="PHPUNIT_RUNNING" value="true"/>
</php>
Laravel5.5(供应商/ laravel /框架/ src目录/照亮/基金/测试/关注/ MakesHttpRequests.php)
public function call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
{
$kernel = $this->app->make(HttpKernel::class);
$files = array_merge($files, $this->extractFilesFromDataArray($parameters));
$symfonyRequest = SymfonyRequest::create(
$this->prepareUrlForRequest($uri), $method, $parameters,
$cookies, $files, array_replace($this->serverVariables, $server), $content
);
$response = $kernel->handle(
$request = Request::createFromBase($symfonyRequest)
);
if ($this->followRedirects) {
$response = $this->followRedirects($response);
}
$kernel->terminate($request, $response);
return $this->createTestResponse($response);
}
很明显,laravel没有将请求发送到远程:
$response = $kernel->handle(
$request = Request::createFromBase($symfonyRequest)
);
你应该使用co欺骗。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.