title: Laravel: How can I assertJson an array
body: <p>I am creating a feature test for a <code>Seminar</code>. Everything is working great; I am trying to update my feature test to account for the seminar dates.</p>
  <p>Each <code>Seminar</code> can have one or many dates, so I am saving these values as a json field:</p>
  <pre><code>// migration:
  
  ...
  
  $table-&gt;json('dates');
  
  ...
  
  </code></pre>
  <p>Here is what my <code>Seminar</code> model looks like:</p>
  <pre><code>// Seminar.php
  
  protected $casts = [
      'dates' =&gt; 'array',
  ];
  </code></pre>
  <p>When saving the seminar, I am returning a json resource:</p>
  <pre><code>if ($seminar-&gt;save()) {
      return response()-&gt;json(new SeminarResource($seminar), 200);
      ...
  </code></pre>
  <p>Using Postman, my <code>Seminar</code> looks a like this:</p>
  <pre><code>...
  
  &quot;capacity&quot;: 100,
  &quot;dates&quot;: [
      &quot;2020-10-15&quot;,
      &quot;2020-10-16&quot;
  ],
  
  ...
  </code></pre>
  <p>So far so good!</p>
  <p>In my test, I am testing that a seminar can be created.</p>
  <pre><code>$http-&gt;assertStatus(201)
  
  -&gt;assertJson([
      'type' =&gt; 'seminars',
      'id' =&gt; (string)$response-&gt;id,
      'attributes' =&gt; [
          'dates' =&gt; $response-&gt;attributes-&gt;dates, // General error: 25 column index out of range
  
  </code></pre>
  <p>I've tried to convert the array to a string, or <code>json_encode</code> the value in the resource. I don't think that's the correct way since I am already casting the value as an array in the model.</p>
  <p>How can I assert that my <code>dates</code> is returning an array?</p>
  <pre><code> +&quot;dates&quot;: array:2 [
        0 =&gt; &quot;2020-10-15&quot;
        1 =&gt; &quot;2020-10-16&quot;
      ]
  </code></pre>
  <p>Thank you for your suggestions!</p>
  <p><strong>EDIT</strong></p>
  <p>When I <code>dd($response-&gt;attributes-&gt;dates);</code> this is what I'm getting (which is correct).</p>
  <pre><code>array:2 [
    0 =&gt; &quot;2020-10-15&quot;
    1 =&gt; &quot;2020-10-16&quot;
  ]
  </code></pre>
  <p>What I'm not sure is how to assert an array like that. Since I'm using faker to generate the date, I don't really know (or care) what the date is, just want to assert that it is in fact an array.</p>
  <p>I've tried something like:</p>
  <pre><code> 'dates' =&gt; ['*'],
  </code></pre>
  <p>However, that just adds another element to the array.</p>
  <p><strong>EDIT 2</strong></p>
  <p>If I make the array a string,</p>
  <pre><code>'dates' =&gt; json_encode($response-&gt;attributes-&gt;dates),
  </code></pre>
  <p>I'll get an error like this:</p>
  <pre><code>
  --- Expected
  +++ Actual
  @@ @@
  
  
  -    'dates' =&gt; '[&quot;2020-10-15&quot;,&quot;2020-10-16&quot;]',
  +    'dates' =&gt; 
  +    array (
  +      0 =&gt; '2020-10-15',
  +      1 =&gt; '2020-10-16',
  +    ),
  </code></pre>
  <p>In my database, the values are stored like this:</p>
  <pre><code>[&quot;2020-10-15&quot;,&quot;2020-10-16&quot;]
  </code></pre>
  <p>My actual test looks like this:</p>
  <pre><code>$http-&gt;assertStatus(201)
              -&gt;assertJsonStructure([
                  'type', 'id', 'attributes' =&gt; [
                      'name', 'venue', 'dates', 'description', 'created_at', 'updated_at',
                  ],
              ])
              -&gt;assertJson([
                  'type' =&gt; 'workshops',
                  'id' =&gt; (string)$response-&gt;id,
                  'attributes' =&gt; [
                      'name' =&gt; $response-&gt;attributes-&gt;name,
                      'venue' =&gt; $response-&gt;attributes-&gt;venue,             
                      'dates' =&gt; $response-&gt;attributes-&gt;dates,
                      'description' =&gt; $response-&gt;attributes-&gt;description,
                      'created_at' =&gt; (string)$response-&gt;attributes-&gt;created_at,
                      'updated_at' =&gt; (string)$response-&gt;attributes-&gt;updated_at,
                  ],
              ]);
  
          $this-&gt;assertDatabaseHas('workshops', [
              'id' =&gt; $response-&gt;id,
              'name' =&gt; $response-&gt;attributes-&gt;name,
              'venue' =&gt; $response-&gt;attributes-&gt;venue,
              'dates' =&gt; $response-&gt;attributes-&gt;dates,
              'description' =&gt; $response-&gt;attributes-&gt;description,
          ]);
  </code></pre>