我使用的是Laravel 5.1 Testsuite。
现在,我用seeJson()方法测试json api。该方法需要一个数组,例如:
->seeJson(['created' => 1]);
但是我的json api总是返回一个json对象或者一个带有json对象的数组,在这个例子中是:
Response::json(['created' => 1], 200);
在上面的例子中,我的json api返回:
{created: 1}
但是seeJson()会精确地查找给定的数组:
[created: 1]
我从来没有通过匹配的测试。我怎么匹配?
我收到一个类似的错误。
无法找到JSON片段("创建":1)在({"创建":"1 "}......]
查看源代码,该错误被写成:
"Unable to find JSON fragment [{$expected}] within [{$actual}]."
所以[
]
没有被包含在搜索
Response::json(['created' => 1], 200);
结果:{"created":1}
当我在seeJson()
方法中转储响应内容时,所有值都在引号中。尝试将测试更改为:
->seeJson(['created' => '1']);
seeJson
进行严格比较,因此:
'created' => 1 (Interpreted as integer)
将失败,因为您的API返回的JSON实际上是
"created":"1" (interpreted as string)