1

I want to test, whether an AJAX response contains the array I'm expecting.
So far so good, not really a great deal.
This is how my array should look like:

array (
  'data' => 
  array (
    0 => 
    array (
      'key1' => 'value1',
      'key2' => 'value2,
    ),
    1 => 
    array (
      'key1' => 'value3',
      'key2' => "value4",
    ),
  ),
)

When I run my test:

$request->assertJson([the array mentioned above]);

The array really looks like that but it fails anyway. Why? because in reality it expects the array twice.
In the comparison window, I see that it expects this:

array (
  'data' => 
  array (
    0 => 
    array (
          'key1' => 'value1',
          'key2' => 'value2,
    ),
    1 => 
    array (
          'key1' => 'value3',
          'key2' => "value4",
    ),
  ),
  0 => 
  array (
          'key1' => 'value1',
          'key2' => 'value2,
  ),
  1 => 
  array (
          'key1' => 'value3',
          'key2' => "value4",
  ),
)

But got the array mentioned above (which would be what I expect too).

When I run $request->assertJSON([]); the test succeeds but this can't be the way it's supposed to work, is it?

TimSch
  • 1,189
  • 1
  • 12
  • 27

1 Answers1

0

This is not a real answer (in terms of solving the underlying problem), but as I'm supposing this to be a bug, I want to share a workaround with people who encounter this problem too:
It's pretty simple. Just store the json into a variable $array = $request->json() (assuming that the response is saved into the $request variable.
Then test the contained array.
$this->assertEquals(EXPECTED_DATA, ARRAY_TO_TEST).

TimSch
  • 1,189
  • 1
  • 12
  • 27