kleijn's avatar

Testing Controller with Auth Guard

Hi,

I'm trying to create unit tests for my rest controllers (with and without auth guard).

How can I mock the authenticate middleware ? Can some please provide me an example.

Roy

0 likes
4 replies
danmatthews's avatar

You can "pretend" to be logged in as a user, using actingAs() on your tests:

$user = \App\User::find($id);
$this->actingAs($user);

$this->get('/my-route'); // Will pass the authentication checks.
1 like
kleijn's avatar

Thanks for your reply.

Can we also mock the database calls somehow? I don't want to rely on the data present in the database for my unit testing.

I'm using Eloquent.

Thanks in advance

kleijn's avatar

Or would you advice to insert -> test -> delete data with these lumen unit tests

kleijn's avatar

any example would be appreciated..

Please or to participate in this conversation.