Laravel 5 PHPUnit - 路由返回了无效的JSON

Cas*_*Cas 5 php phpunit json routes laravel

路线

Route::group(array('prefix' => 'api'), function() {
   Route::resource('test', 'TestController', array('only' => array('index', 'store', 'destroy', 'show', 'update')));
});
Run Code Online (Sandbox Code Playgroud)

调节器

public function store(Request $request) {
    return response()->json(['status' => true]);
}
Run Code Online (Sandbox Code Playgroud)

单元类

public function testBasicExample() {
    $this->post('api/test')->seeJson(['status' => true]);
}
Run Code Online (Sandbox Code Playgroud)

PHPUnit结果:

1) ExampleTest::testBasicExample

路由返回了无效的JSON.

也许是一个例外被抛出?有人看到问题吗?

Pan*_*lis 5

问题是CSRF 令牌

您可以使用trait来禁用中间件WithoutMiddleware

<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;

class ExampleTest extends TestCase
{
    use WithoutMiddleware;

    //
}
Run Code Online (Sandbox Code Playgroud)

或者,如果您只想禁用一些测试方法的中间件,您可以withoutMiddleware从测试方法中调用该方法:

<?php

class ExampleTest extends TestCase
{
    /**
     * A basic functional test example.
     *
     * @return void
     */
    public function testBasicExample()
    {
        $this->withoutMiddleware();

        $this->visit('/')
             ->see('Laravel 5');
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 有没有办法查看抛出的异常? (4认同)