php - Laravel Dusk loginAs 不工作,当使用 DatabaseMigration 特性时

标签 php laravel laravel-dusk

我用 Laravel Dusk 编写测试,不需要清除数据库中以前的测试数据,所以一切正常。 当在 DuskTestCase.php 文件中添加 use DatabaseMigrations; 以确保清除所有以前的测试数据时,我的问题就开始了。 但问题是 loginAs 不工作。所以我的很多测试都失败了,因为我的测试被重定向到应用程序登录页面! 任何帮助将不胜感激。

这是我的测试代码:

<?php

namespace Tests\Browser;

use App\Models\Brand;
use App\Models\User;
use Laravel\Dusk\Browser;
use Tests\Browser\Components\DashboardCategoryForm;
use Tests\DuskTestCase;

class DashboardBrandsTest extends DuskTestCase
{
    /**
     * A Dusk test example.
     *
     * @return void
     * @throws \Exception
     * @throws \Throwable
     */
    public function testBrandList()
    {
        $user = factory(User::class)->create();

        $this->browse(function (Browser $browser) use ($user) {
            $browser->loginAs($user)
                ->visitRoute('dashboard.brands.index')
                ->assertSee('‌Brands')
                ->assertSee('id')
                ->assertSee('name')
                ->assertSee('image')
                ->assertSee('updated_at')
                ->assertSee('operations');
        });
    }

    /**
     * @throws \Exception
     * @throws \Throwable
     */
    public function testCreateUpdateDeleteBrand()
    {
        $user = factory(User::class)->create();

        $this->browse(function (Browser $browser) use ($user) {
            $expectedName = 'Brand ' . $user->id;
            $expectedNameEdited = $expectedName . ' Edited';
            $expectedShortDesc = 'This is a little desc for test.';
            $expectedShortDescEdited = 'Edited desc';
            $expectedImage = __DIR__ . '/photos/test.jpg';

            $browser->loginAs($user)
                ->visitRoute('dashboard.brands.index')
                ->click('@new-category')
                ->assertRouteIs('dashboard.brands.create')
                ->within(new DashboardCategoryForm, function ($browser)
                use ($expectedName, $expectedShortDesc, $expectedImage) {
                    $browser->fillCategory(
                        $expectedName,
                        $expectedShortDesc,
                        $expectedImage
                    );
                })
                ->click('@submit')
                ->assertRouteIs('dashboard.brands.index')
                ->assertSee($expectedName);

            $latestRecord = Brand::orderBy('updated_at', 'desc')->first();

            $browser
                ->click('@edit-category' . $latestRecord->id)
                ->within(new DashboardCategoryForm, function ($browser)
                use ($expectedName, $expectedShortDesc) {
                    $browser->checkCategory(
                        $expectedName,
                        $expectedShortDesc
                    );
                })
                ->within(new DashboardCategoryForm, function ($browser)
                use ($expectedNameEdited, $expectedShortDescEdited, $expectedImage) {
                    $browser->fillCategory(
                        $expectedNameEdited,
                        $expectedShortDescEdited,
                        $expectedImage
                    );
                })
                ->click('@submit')
                ->assertRouteIs('dashboard.brands.index')
                ->assertSee($expectedNameEdited)
                ->click('@edit-category' . $latestRecord->id)
                ->within(new DashboardCategoryForm, function ($browser)
                use ($expectedNameEdited, $expectedShortDescEdited) {
                    $browser->checkCategory(
                        $expectedNameEdited,
                        $expectedShortDescEdited
                    );
                })
                ->click('@submit')
                ->assertRouteIs('dashboard.brands.index')
                ->click('@delete-category' . $latestRecord->id)
                ->acceptDialog()
                ->pause(500)
                ->assertDontSee($expectedNameEdited);
        });
    }
}

这是我的 env.dusk.local:

APP_NAME=Laravel
APP_ENV=testing
APP_KEY=base64:EOMuHiEoMcS0YvJQq+b/bSVm0kXfJwNnNFJ7WOQwWwQ=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://site.local

DB_CONNECTION=mysql
DB_HOST=10.5.0.7
DB_PORT=3306
DB_DATABASE=db_test
DB_USERNAME=root
DB_PASSWORD=secret

BROADCAST_DRIVER=log
CACHE_DRIVER=database
SESSION_DRIVER=database
SESSION_LIFETIME=120
QUEUE_DRIVER=sync

REDIS_HOST=10.5.0.9
REDIS_PASSWORD=secret
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

最佳答案

当您开始编辑类别时,这意味着您希望数据库中已经有数据。 DatabaseMigrations 创建您的数据库结构,但它不会为您的数据库提供种子,因此如果您尝试登录,则数据库中可能没有用户可供您登录。<​​/p>

我发现解决此问题的最佳方法是在每次测试开始时添加播种,尽管这确实会使它们变得相当慢。我尝试了多种方法,这是我唯一可以开始工作的方法。

    public function setUp()
{
    parent::setUp();
    $this->artisan('db:seed');
}

关于php - Laravel Dusk loginAs 不工作,当使用 DatabaseMigration 特性时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49842337/

相关文章:

PHP:如何在重新排序数组时对 1 个数组中的 3 个数组进行排序

php - 如何使用javascript将相同的变量从php发送到两个不同的php页面

php - 如何在 PHP 中连接用户定义的变量并将结果插入 MySQL 表

php - 在 MySQL 的 WHERE EXISTS 查询中使用选定的列

laravel - Laravel Dusk Facebook\WebDriver\Exception\SessionNotCreatedException : session not created: Chrome version must be between 70 and 73 上的错误

php - 从 array_rand 中排除选举

laravel - 检测 Eloquent Updated 事件期间的特定更改

php - Laravel - 获取网站唯一访客数量

Laravel 黄昏 : test file upload with Dropzone. js

css - 断言每个选定的 div 元素