shaikh709's avatar

ActingAs Not working.

I'm trying to test if user can see dashboard after login however it works perfectly fine in browser but when I run phpunit. I get status code 302. It redirect to login page. I'm using sqlite for testing

    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="DB_CONNECTION" value="sqlite" />
        <env name="DB_DATABASE" value=":memory:"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
    </php>

and in this is my test functions

    /** @test */
    public function a_authenticated_admin_can_see_dashboard()
    {
        $admin = factory('App\Admin')->create(['password' => 'secret']);

        $this->actingAs($admin)
                ->get(route('admin.index'))
                ->assertSee('Admin Dashboard');
    }

Please help. Thank you.

0 likes
1 reply
shaikh709's avatar
shaikh709
OP
Best Answer
Level 15

Ohh sorry. I'm an idiot. I'm using custom guard i changed my code to below code.

Issue was, i'm using custom guard for admin user. When I inspected actingAs method then I saw it uses default guard to login if nothing provided.

    /** @test */
    public function a_authenticated_admin_can_see_dashboard()
    {
        $admin = factory('App\Admin')->create(['password' => 'secret']);

        $this->actingAs($admin, 'admin')
                ->get(route('admin.index'))
                ->assertSee('DASHBOARD');
    }

Thanks

2 likes

Please or to participate in this conversation.