gpt4 book ai didi

php - Laravel 的 TestCase 方法 seeJson() 只查找数组

转载 作者:IT王子 更新时间:2023-10-29 00:13:29 26 4
gpt4 key购买 nike

我正在使用 Laravel 5.1 测试套件。

现在,我使用方法 seeJson() 测试我的 json api。此方法需要一个数组,例如:

->seeJson(['created' => 1]);

但我的 json api 总是返回一个 json 对象 或一个带有 json 对象的数组,在这种情况下:

Response::json(['created' => 1], 200);

在上面的例子中,我的 json api 返回:

{created: 1}

但是 seeJson() 会精确查找给定的数组:

[created: 1]

我的测试从来没有通过比赛。我该如何匹配?

最佳答案

我收到了类似的错误。

Unable to find JSON fragment ["created":1] within [{"created":"1"}......]

查看源码,那个错误是这样写的:

"Unable to find JSON fragment [{$expected}] within [{$actual}]."

因此 [ ] 未包含在搜索中

Response::json(['created' => 1], 200);

结果: {"created":1}

当我在 seeJson() 方法中转储响应内容时,所有值都在引号中。尝试将测试更改为:

->seeJson(['created' => '1']);

关于php - Laravel 的 TestCase 方法 seeJson() 只查找数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32276351/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com