Laravel 5 PHPUnit - 从路由

时间:2015-12-08 10:16:03

标签: php json laravel routes phpunit

路线

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

控制器

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

单元类

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

PHPUnit结果:

1)ExampleTest::testBasicExample

  

路由返回了无效的JSON。

也许抛出异常?有人看到问题吗?

1 个答案:

答案 0 :(得分:4)

问题是 CSRF令牌

您可以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');
    }
}