Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel's TestCase method seeJson() only looks for array

I'm using Laravel 5.1 Testsuite.

Now, I test my json api with the method seeJson(). This method expects an array, such as:

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

But my json api always returns a json object or an array with json objects with, in this case:

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

In the case above, my json api returns:

{created: 1}

But seeJson() looks for the given array exactly:

[created: 1]

I never get my tests pass with a match. How can I match?

like image 839
schellingerht Avatar asked Aug 28 '15 17:08

schellingerht


2 Answers

I received a similar error.

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

Looking at the source code, that error is written as:

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

So the [ ] are not being included in the search

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

results in: {"created":1}

When I dump out the response content within the seeJson() method, all values are in quotes. Try changing your test to:

->seeJson(['created' => '1']);
like image 62
neal Avatar answered Nov 18 '22 22:11

neal


seeJson make strict comparisons, so:

'created' => 1 (Interpreted as integer)

Will fail because the JSON returned by your API is actually

"created":"1" (interpreted as string)
like image 3
David L Avatar answered Nov 19 '22 00:11

David L