mercredi 6 décembre 2017

Error testing JSON fragment in Laravel

In Laravel 5.5, I have a test using assertJsonFragment:

$this->get('/tagAutoComplete?tag=foo')
    ->assertJsonFragment([
      'tag' => 'foobar'
    ]);

This is the tagAutoComplete method called by the test:

public function tagAutoComplete(Request $request)
{
  $tag = $request->tag;
  $tags = Tag::where('tag', 'like', '%' . $tag . '%')->get();

  return response()->json($tags);
}

However when I run the test it produces the error:

Unable to find JSON fragment:

["tag":"foobar"]

within

[[{"created_at":"2017-12-06 11:05:55","id":1,"slug":"foobar","tag":"foobar","updated_at":"2017-12-06 11:05:55"}]].
Failed asserting that false is true.

There's clearly something wrong with the format of the response, but I can't figure out how to get at it. The tag is there, but not being found.

Aucun commentaire:

Enregistrer un commentaire