Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save devmsh/23ab1238681b12f3c06f90dc24eccf6d to your computer and use it in GitHub Desktop.
Write better Laravel tests 4
<?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