iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🐡

Authenticating laravel-admin in Laravel Dusk Browser Integration Tests

に公開

Environment

  • Used chromedriver-mac-intel driver on local macOS
  • Laravel version 8.0, laravel-admin version 1.8

Refer to Laravel Dusk for setup.

Execution

./vendor/laravel/dusk/bin/chromedriver-mac-intel

Start the Headless ChromeDriver.

php artisan --env=testing serve

Start the web server to be tested.

php artisan test tests/Browser

Execute tests.

<?php

namespace Tests\Browser;

use App\Models\Administrator;
use Laravel\Dusk\Browser;
use Tests\DuskTestCase;
use Illuminate\Database\Eloquent\Factories\Factory;

class ExampleTest extends DuskTestCase
{
    /**
     * A basic browser test example.
     *
     * @return void
     */
    public function testBasicExample()
    {
        $class = config('admin.auth.providers.admin.model');
        $admin = Factory::factoryForModel($class)->create();

        $this->browse(function (Browser $browser) use ($admin) {
            $browser->loginAs($admin, config('admin.auth.guard'))
                ->visit('/')
                ->assertSee($admin->name);
        });
    }
}

Test code to log in and verify if the username is displayed.

However, this alone will not resolve Admin::user() being called in the Controller, so set it as follows:

Admin::guard()->setUser($admin);

Discussion