Skip to content

Instantly share code, notes, and snippets.

@devmsh
Created February 10, 2020 08:04
Show Gist options
  • Select an option

  • Save devmsh/f87dd2ccec94d22f8ec80608a1660199 to your computer and use it in GitHub Desktop.

Select an option

Save devmsh/f87dd2ccec94d22f8ec80608a1660199 to your computer and use it in GitHub Desktop.
Write better Laravel tests 3
<?php
// add this macro to your service provider
TestResponse::macro('assertJsonPaths', function ($path, $expected) {
foreach ($this->json($path) as $real) {
PHPUnit::assertEquals($expected, $real);
}
return $this;
});
// then refactor the test
public function test_users_can_get_their_categories_list()
{
factory(Category::class, 5)->create();
$user = factory(User::class)->create();
Passport::actingAs($user);
$this->get('api/categories')
->assertSuccessful()
->assertJsonCount(5)
->assertJsonPaths('*.user_id', $user->id)
->assertJsonStructure([
[
'id',
'name',
'type',
],
]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment