Created
February 10, 2020 08:04
-
-
Save devmsh/f87dd2ccec94d22f8ec80608a1660199 to your computer and use it in GitHub Desktop.
Write better Laravel tests 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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