In Laravel's unit test, I can test a JSON API like that:

$this->post('/user', ['name' => 'Sally'])
    ->seeJson([
        'created' => true,
    ]);

But what if I want to use the response. How can I get the JSON response (as an array) using $this->post()?

推荐答案

目前在5.3中,这是有效的.

$content = $this->get('/v1/users/1')->response->getContent();

但是,它确实打破了这个链条,因为response返回的是响应,而不是测试运行程序.所以,在获取响应之前,您应该做出可链接的断言,就像这样...

$content = $this->get('/v1/users/1')->seeStatusCode(200)->response->getContent();

Laravel相关问答推荐

Hostinger Laravel网站未显示错误

Livewire 3软件包开发

如何让 Laravel 的 Collection 表现得像一个流?

使用 fetch api 时 Laravel 控制器不存在错误

如何在 laravel 5.3 中验证没有 auth:api 中间件的用户?

在 laravel 4.2 中,用户 'root'@'localhost' 的 Laravel 访问被拒绝(使用密码:YES)

安卓 retrofit |发布自定义对象(将 json 发送到服务器)

使用 Laravel 和 Passport 验证失败时响应状态码 401?

laravel 密码盐存储在哪里?

如何使用 Laravel 模型访问数据库视图?

Laravel 5.x 中 onUpdate / onDelete 的可用操作

在 Laravel 5 中扩展请求类

在 AWS 上找不到 Laravel 5 类Collective\Html\HtmlServiceProvider

Laravel 分页方法不适用于 map 集合?

Laravel 5.4 有时验证规则不起作用

laravel 搜索多个以空格分隔的单词

无法打开laravel.log:无法打开流

在 Laravel artisan 命令中使用详细

如何在 Laravel 5.2 中使用 OR 条件将多个参数传递给中间件

如何判断是否在laravel中使用了分页