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

标签 php json laravel routes phpunit

路线

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/

相关文章:

php - 如果在 WooCommerce 中选中结账自定义复选框,请更改用户角色

php - 检查 SQL 条目中区分大小写条目的更好方法

javascript - Angular JS 和 Spring - 如何过滤 JSON 对象

javascript - 从 ajax 返回数据会产生奇怪的对象

jQuery .ajax post 因大型 JSON 对象而失败

Ajax 调用 Laravel 路由

java - 译者: pseudo-code to a concrete language (PHP, JAVA)库建议

php - 使用 SimpleXML 获取 "Form"元素,该元素具有类型为 "input"的 "password"标签?

php - Laravel Auth::user()->roles()->get() 为空

php - Laravel/MYSQL (Eloquent) - 查询大表