路线
Route::group(array('prefix' => 'api'), function() {
Route::resource('test', 'TestController', array('only' => array('index', 'store', 'destroy', 'show', 'update')));
});
Controller
public function store(Request $request) {
return response()->json(['status' => true]);
}
单元类
public function testBasicExample() {
$this->post('api/test')->seeJson(['status' => true]);
}
PHPUnit 结果:
1) ExampleTest::testBasicExample
Invalid JSON was returned from the route.
也许抛出了异常?有人看到问题了吗?
最佳答案
问题是 CSRF token 。
你可以 disable the middleware通过使用 WithoutMiddleware
特性:
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
class ExampleTest extends TestCase
{
use WithoutMiddleware;
//
}
或者,如果您只想为几个测试方法禁用中间件,您可以从测试方法中调用 withoutMiddleware
方法:
<?php
class ExampleTest extends TestCase
{
/**
* A basic functional test example.
*
* @return void
*/
public function testBasicExample()
{
$this->withoutMiddleware();
$this->visit('/')
->see('Laravel 5');
}
}
关于php - Laravel 5 PHPUnit - 从路由返回无效的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34153252/