framework icon indicating copy to clipboard operation
framework copied to clipboard

seeJson() never match

Open schellingerht opened this issue 11 years ago • 6 comments

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 line like below:

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.

schellingerht avatar Sep 01 '15 08:09 schellingerht

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

GrahamCampbell avatar Sep 01 '15 10:09 GrahamCampbell

@GrahamCampbell Thanks for your reply, but seeJson() expects an array.

ErrorException: Argument 1 passed to Illuminate\Foundation\Testing\TestCase::seeJson() must be of the type array, object given, called in /Applications/XAMPP/xamppfiles/htdocs/w/server/tests/UserOrganisationTest.php on line 7 and defined

schellingerht avatar Sep 01 '15 11:09 schellingerht

Just literally check the response body is equal. Just because it's a json response, doesn't mean you need to use fancy methods.

GrahamCampbell avatar Sep 01 '15 11:09 GrahamCampbell

@GrahamCampbell I know, but why tells the doc that there are json-methods: seeJson(), seeJsonContains(), etc for testing json output?

See this: http://laravel.com/docs/5.1/testing#testing-json-apis

schellingerht avatar Sep 01 '15 12:09 schellingerht

Ok, I'll check via another way the json output, but why are there new methods in L5.1 testsuite if you say they're too fancy to use?

schellingerht avatar Sep 01 '15 20:09 schellingerht

I know, but why tells the doc that there are json-methods: seeJson(),

Yeh, sure there are, but it doesn't mean they're suitable for every use case ever. you need to make you're own judgement calls. Everything's not one size fits all.

GrahamCampbell avatar Sep 01 '15 22:09 GrahamCampbell