Pest PHP / PHPUnit : run test with role

1.2k views Asked by At

I'm using Pest PHP (with Laravel) and i'm trying to run test with a given role but i already have 403 status (Forbidden). I'm using spatie/laravel-permission to manage roles.

This is my route :

Route::get('liste', IndexAccessController::class)->name('access.index')->middleware('role:team');

And this is my Pest PHP Test:

beforeEach(function () {
    $me = factory(User::class)->create();
    $role = \Spatie\Permission\Models\Role::where('name', 'team')->first();
    $me->assignRole($role);

    $this->actingAs($me);
});

test('has index page', function () {
    $response = $this->get(route('access.index'));
    $response->assertStatus(200);
});

When I debug to get the user's roles Laravel tells me that he doesn't have any.

Thanks a lot

0

There are 0 answers