David Nguyen
David Nguyen

Reputation: 8528

Laravel 5.1 unit testing authenticated user

So after reading through the documentation a couple of times, I'm still confused as to how you can authenticate as a user for unit testing in 5.1. Previously in 4.2 you could use:

$user = new User(array('name' => 'John'));
$this->be($user);

In 5.1 the example given is:

$user = factory('App\User')->create();
$this->actingAs($user)

Isn't the factory just returning a User? If I attempt to use $user = new User(array('name' => 'John')); I get an error because actingAs expects an Authenticatable. So how do you authenticate as an existing user?

Upvotes: 1

Views: 2442

Answers (1)

Ilyas Serter
Ilyas Serter

Reputation: 840

$user = new User(..); doesn't give you an existing user either. Try $user = User::find(1) to become an existing member. If you meant testing as a non-existing member, you can just include the DatabaseTransactions trait which rolls back everything that was created during the test.

Upvotes: 2

Related Questions