在开始之前,我想说我已经看过Failed asserting the HTTP status is 200 not 500以及Failed asserting the HTTP status code is 200 not 500。不要让名字欺骗你;他们是2个不同的职位。这两个帖子都超过 9 个月,没有好的答案。
我在测试路线时遇到了一些麻烦。所以,我Laravel 5
在我的 Mac Mini 上创建了一个新项目,正在运行OS X Yosemite 10.10.2
. 我正在制作一个 API。所以,我首先开始制作 API 的后端部分。我决定我想用它PHPUnit
来为这个项目实现 TDD。我的版本PHPUnit
是4.0.20
. 我正在运行PHP 5.5.14
,并且正在使用命令行工具Laravel
中打包的服务器。artisan
现在,我开始确保我可以连接到服务器。所以,我的第一个测试看起来像:
public function testGetMessagesResponse()
{
$response = $this->call('GET', 'Messages');
$this->assertEquals(200, $response->getStatusCode());
}
这通过就好了。但是,当我转到 POST 请求时,它失败了。这是我的第二个测试的样子:
public function testPostMessagesResponse()
{
$response = $this->call('POST', 'Messages');
$this->assertEquals(200, $response->getStatusCode());
}
此测试失败。这是输出的样子:
1) MessagesTest::testPostMessagesResponse
Failed asserting that 500 matches expected 200.
在我的 routes.php 文件中,我确实注册了这条路线:
Route::resource('Messages', 'MessagesController');
在我的 MessagesController 中,我拥有所有必需的方法:
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class MessagesController extends Controller {
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
return 'response';
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
return 'response';
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store()
{
return 'response';
}
/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
public function show($id)
{
return 'response';
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return Response
*/
public function edit($id)
{
return 'response';
}
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
public function update($id)
{
return 'response';
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
return 'response';
}
}
所以,目前,不管 HTTP 动词是什么,我都应该得到'response'
. 现在,我决定用 curl 验证测试:
curl -X POST http://myapp.com/Messages
这将返回 Laravel '哎呀!页。所以确实存在某种服务器错误,但我不确定要寻找什么。应用程序流程看起来不错,但显然无法正常工作。这是我的 Laravel 日志中的堆栈跟踪:
[timestamp] local.ERROR: exception 'Illuminate\Session\TokenMismatchException' in /Users/username/Sites/project/storage/framework/compiled.php:2375
Stack trace:
#0 /Users/username/Sites/project/app/Http/Middleware/VerifyCsrfToken.php(17): Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure))
#1 /Users/username/Sites/project/storage/framework/compiled.php(8858): App\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure))
#2 /Users/username/Sites/project/storage/framework/compiled.php(11990): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#3 /Users/username/Sites/project/storage/framework/compiled.php(8858): Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#4 /Users/username/Sites/project/storage/framework/compiled.php(10696): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#5 /Users/username/Sites/project/storage/framework/compiled.php(8858): Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#6 /Users/username/Sites/project/storage/framework/compiled.php(11696): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#7 /Users/username/Sites/project/storage/framework/compiled.php(8858): Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure))
#8 /Users/username/Sites/project/storage/framework/compiled.php(11645): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#9 /Users/username/Sites/project/storage/framework/compiled.php(8858): Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request), Object(Closure))
#10 /Users/username/Sites/project/storage/framework/compiled.php(2411): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#11 /Users/username/Sites/project/storage/framework/compiled.php(8858): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#12 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#13 /Users/username/Sites/project/storage/framework/compiled.php(8849): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#14 /Users/username/Sites/project/storage/framework/compiled.php(1862): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#15 /Users/username/Sites/project/storage/framework/compiled.php(1852): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#16 /Users/username/Sites/project/public/index.php(53): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#17 /Users/username/Sites/project/server.php(21): require_once('/Users/username/Site...')
#18 {main}
我对 Laravel 很陌生,所以我不确定如何真正深入研究并使用堆栈跟踪来找出问题所在。我也尝试过没有Route::resource
,但这并不能缓解我的情况。我的 GET 仍然可以正常工作 ( Route::get
),但我的 POST ( Route::post
) 仍然出现同样的 500 错误。.../compiled.php 中包含第 2375 行的函数(来自堆栈跟踪顶部抛出的异常)由 Laravel 生成,如下所示:
public function handle($request, Closure $next)
{
if ($this->isReading($request) || $this->tokensMatch($request)) {
return $this->addCookieToResponse($request, $next($request));
}
throw new TokenMismatchException();
}
那时,我尝试编辑 POST 测试,因为我实际上并没有发送任何数据。所以这是我修改后的第二个测试:
public function testPostMessagesResponse()
{
$response = $this->call('POST', 'Messages', array("key" => "value"));
$this->assertEquals(200, $response->getStatusCode());
}
我收到相同的错误消息。我觉得我错过了一些简单的东西,但我无法弄清楚。任何帮助将不胜感激。