I'm sending a post request in a test case, and I want to assert that a specific element, let's say with key 'x' exists in the response. In this case, I can't say seeJson(['x' => whatever]); because the value is unknown to me. and for sure, I can't do it with seeJson(['x']);.

有没有办法解决这个问题?

如果重要的话:

推荐答案

Although it's not optimal at all, I chose to use this code to test the situation:

$this->post(URL, PARAMS)->see('x');

X是一个假想的名称,而实际的元素键出现在其余数据中的可能性很小.否则,这种讨厌的解决方法就不实用了.

UPDATE:

以下是正确执行此操作的解决方案:

public function testCaseName()

{
    $this->post(route('route.name'), [
        'param1' => 1,
        'param2' => 10,
    ], [
        'headers_if_any' => 'value'
    ]);

    $res_array = (array)json_decode($this->response->content());

    $this->assertArrayHasKey('x', $res_array);
}

Laravel相关问答推荐

密码确认在Livewire中不匹配

在 Laravel 中清除 Routes&config:cache 后未定义的常量

使用Laravel Blade Formatter和PHP Intelephense进行 VSCode 格式化

Laravel 如何处理来自浏览器的 PUT 请求?

Laravel Eloquent:访问属性和动态表名

Laravel 5.4 LengthAwarePaginator

Laravel - 如何恢复本地存储符号链接并刷新

Laravel 验证 - 输入必须是数组中的项目之一

设置 Laravel 5.4 以使用 React

调用未定义的函数 mb_strimwidth

如何在包中安排 Artisan 命令?

Laravel 同步错误

Laravel 迁移 - 删除列

调用未定义的方法 Illuminate\Routing\Route::get()

Laravel php artisan 服务于模仿 HTTPS

assets资源不引用公用文件夹(Laravel)

laravel如何访问具有表编号名称的列?

Laravel 删除集合中的第一项

如何使 Laravel (Blade) 文本字段只读

错误:找不到模块 'webpack/lib/rules/DescriptionDataMatcherRulePlugin' 需要堆栈: