Dusk: loginAs creates an "Exception: User resolver has not been set."

Created on 16 Jan 2018  路  11Comments  路  Source: laravel/dusk

I am not able to use the the loginAs method cause it always throws this error :

1) Tests\Browser\DashboardControllerTest::testPageDisplay Exception: User resolver has not been set.

````php
class DashboardControllerTest extends DuskTestCase
{
use DatabaseMigrations;

/**
 * Test the Dashboard Display
 *
 * @return void
 * @throws \Exception
 * @throws \Throwable
 */
public function testPageDisplay()
{
    $this->browse(function (Browser $browser) {
        $browser->on(new Login())->login()->visit(new Dashboard())->assertSee('back');
    });
}

}

````

with this Page :

````php
class Login extends BasePage
{
/**
* Get the URL for the page.
*
* @return string
*/
public function url()
{
return '/login';
}

/**
 * Abstract the Login functionality
 *
 * @param  \Laravel\Dusk\Browser $browser
 * @return void
 */
public function login(Browser $browser)
{
    $browser->loginAs(User::where('email', 'admin@xxx')->firstOrFail);
}

````

Versions :
-

  • Laravel v5.5.29
  • Dusk v2.0.8

Result
-
````
PHPUnit 6.5.5 by Sebastian Bergmann and contributors.

E. 2 / 2 (100%)
Time: 1.45 seconds, Memory: 14.00MB
There was 1 error:

1) Tests\Browser\DashboardControllerTest::testPageDisplay
Exception: User resolver has not been set.

/Users/xxx/Workspace/xxx/vendor/laravel/dusk/src/TestCase.php:244
/Users/xxx/Workspace/xxx/vendor/laravel/dusk/src/TestCase.php:49
/Users/xxx/Workspace/xxx/vendor/laravel/dusk/src/Concerns/InteractsWithAuthentication.php:17
/Users/xxx/Workspace/xxx/tests/Browser/Back/DashboardControllerTest.php:25
/Users/xxx/Workspace/xxx/vendor/laravel/dusk/src/TestCase.php:92
/Users/xxx/Workspace/xxx/tests/Browser/Back/DashboardControllerTest.php:26
````

Do you have any idea ?

Most helpful comment

Hi! I 've had the same issue few hour ago. And the way I've solved this problem was renaming login() method to any other name(Ex.: loginUser()). As I understood - Laravel uses some other login() method instead of method written by me. Not sure if it helps you, but you can try

All 11 comments

Out of curiosity, what happens if you rewrite your test to be something like:

$this->browse(function (Browser $browser) {
    $user = User::where('email', 'admin@xxx')->firstOrFail();
    $dashboardRouteString = '/dashboard'; //Change this as necessary
    $browser->loginAs($user)->visit($dashboardRouteString)->assertSee('back');
});

@dbudwin This code is working

php $this->browse(function (Browser $browser) { $user = User::where('email', 'admin@xxxx')->firstOrFail(); $browser->loginAs($user)->visit(new Dashboard())->assertSee('back'); });

@deleugpn This one is still not working even after I updated laravel/framework (v5.5.29 => v5.5.31)

php $this->browse(function (Browser $browser) { $browser->on(new Login())->login()->visit(new Dashboard())->assertSee('back'); });

````bash
There was 1 error:

1) Tests\Browser\DashboardControllerTest::testPageDisplay
Exception: User resolver has not been set.

/Users/ben/Workspace/focal/vendor/laravel/dusk/src/TestCase.php:244
/Users/ben/Workspace/focal/vendor/laravel/dusk/src/TestCase.php:49
/Users/ben/Workspace/focal/vendor/laravel/dusk/src/Concerns/InteractsWithAuthentication.php:17
/Users/ben/Workspace/focal/tests/Browser/Back/DashboardControllerTest.php:26
/Users/ben/Workspace/focal/vendor/laravel/dusk/src/TestCase.php:92
/Users/ben/Workspace/focal/tests/Browser/Back/DashboardControllerTest.php:27
````
How can I abstract my login methods ?

Do you have a typo in your first post? It seems you're accessing the attribute firstOrFail, where you should access the method.

@deleugpn I tested both, none worked

Have stumbled on same issue. Any luck?

Nope

Hi! I 've had the same issue few hour ago. And the way I've solved this problem was renaming login() method to any other name(Ex.: loginUser()). As I understood - Laravel uses some other login() method instead of method written by me. Not sure if it helps you, but you can try

@IrynaMlshvk thank you, it worked.

Works correctly with the @IrynaMlshvk tip. Thanks

Thanks @IrynaMlshvk for the tip. Just ran into the issue and renaming the method fixed it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dellow picture dellow  路  4Comments

digitlimit picture digitlimit  路  4Comments

muswanto picture muswanto  路  3Comments

ahtinurme picture ahtinurme  路  6Comments

susanBuck picture susanBuck  路  6Comments