[英]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.