【问题标题】:Laravel 5 PHPUnit - Invalid JSON was returned from the routeLaravel 5 PHPUnit - 从路由返回了无效的 JSON
【发布时间】:2016-03-13 05:13:13
【问题描述】:

路线

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。

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

【问题讨论】:

    标签: php json laravel routes phpunit


    【解决方案1】:

    问题在于 CSRF 令牌

    您可以通过使用 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');
        }
    }
    

    【讨论】:

    • 有没有办法查看抛出的异常?
    • 查看异常: $this->json('POST', 'api/test'); dd($this->response->getContent());
    • 流明中没有中间件。有人知道怎么安装吗?
    • 对于流明,use Laravel\Lumen\Testing\WithoutMiddleware;@KirenSiva
    猜你喜欢
    • 2017-09-05
    • 1970-01-01
    • 2017-03-18
    • 2015-12-10
    • 2017-10-13
    • 2018-02-11
    • 2020-11-11
    • 1970-01-01
    • 2019-05-22
    相关资源
    最近更新 更多