Created
February 10, 2020 08:05
-
-
Save devmsh/23ab1238681b12f3c06f90dc24eccf6d to your computer and use it in GitHub Desktop.
Write better Laravel tests 4
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 method to your TestCase | |
| public function passportAs($user, $scopes = [], $guard = 'api') | |
| { | |
| Passport::actingAs($user, $scopes, $guard); | |
| return $this; | |
| } | |
| // then refacor the test again | |
| public function test_users_can_get_their_categories_list() | |
| { | |
| factory(Category::class, 5)->create(); | |
| $user = factory(User::class)->create(); | |
| $this->passportAs($user) | |
| // if you did not use passport scope you can also use this | |
| // ->actingAs($user,'api') | |
| ->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