Skip to content
Advertisement

Is it possible to implement actingAs() function in Laravel if I created another User model?

I’m trying to implement some Laravel tests. I would like to test some authenticated routes, but I’m not using the default user model. I created a new one. Is there a way for me to use the ->actingAs() function?

class HomeTest extends TestCase
{
    /**
     * @test
     */
    public function first_test()
    {

        $user = factory(Usuario::class)->make();
        
        $response = $this
                         ->withoutExceptionHandling()
                         ->actingAs($user)
                         ->get('/ead');
        
        $response->assertStatus(200);
    }
}

The parameter that is received is the IlluminateContractsAuthAuthenticatable

/**
     * Set the currently logged in user for the application.
     *
     * @param  IlluminateContractsAuthAuthenticatable  $user
     * @param  string|null  $driver
     * @return $this
     */
    public function actingAs(UserContract $user, $driver = null)
    {
        return $this->be($user, $driver);
    }

Advertisement

Answer

In any object oriented languages interfaces are some structure/syntax that help developers to use their own classes and just obey the rules(defining what functions should be in your class). Authentictable is one of laravel’s interfaces that helps you define your own User model. Just implement this interface and that’s all. You can use any function that uses this type of interface.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement