路线

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


也许引发了异常?有人看到问题了吗?

最佳答案

问题是 CSRF token

您可以使用WithoutMiddleware特性来disable the middleware:

<?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');
    }
}

10-04 15:14