【问题标题】:How to test authentication using Laravel Dusk? [closed]如何使用 Laravel Dusk 测试身份验证? [关闭]
【发布时间】:2019-12-22 23:40:34
【问题描述】:

我正在使用 Laravel Dusk 设置 e2e 测试,但在试图弄清楚如何测试 Laravel 的身份验证时遇到了问题。

我正在使用DatabaseMigrations trait 迁移数据库,并用 1 条记录填充它(运行模拟用户的 setUp 方法)。我尝试使用loginAs() 方法以这个特定用户登录,但它没有让我登录。

我的AuthenticationTest.php 文件

  use DatabaseMigrations;
    public function setUp(): void
    {
        parent::setUp();
        factory('App\User')->create();
    }


    public function an_authenticated_user_will_see_an_account_button()
    {
        $this->browse(function (Browser $browser) {
            $browser
                ->loginAs(User::find(1))
                ->visit('http://recipemanager.test/recipes')
                ->assertSee('Account');
        });
    }

我的dusk routes

|        | GET|HEAD | _dusk/login/{userId}/{guard?} |                  | Laravel\Dusk\Http\Controllers\UserController@login                     | web          |
|        | GET|HEAD | _dusk/logout/{guard?}         |                  | Laravel\Dusk\Http\Controllers\UserController@logout                    | web          |
|        | GET|HEAD | _dusk/user/{guard?}           |                  | Laravel\Dusk\Http\Controllers\UserController@user                      | web          |

请注意,我没有接触任何控制器,与 Laravel Dusk 相关的文件!

我没有使用测试数据库进行测试,我知道这很糟糕,我正在进入它,不要恨我。

重现错误的步骤(您需要有 XAMPP):
1.Clone the repo
2.切换到分支account
2.配置.env file以匹配您的数据库
3. 确保配置vhosts Apache 文件和etc/hosts 文件,以便您可以访问recipemanager.test 作为主路由(127.0.0.1 recipemanager.test
4.运行php artisan dusk

错误: 1)Tests\Browser\AuthenticationTest::an_authenticated_user_will_see_an_account_button Did not see expected text [Account] within element [body]. Failed asserting that false is true.

【问题讨论】:

    标签: php laravel phpunit laravel-dusk


    【解决方案1】:

    我通过访问食谱visit('/recipes') 解决了这个问题。问题出在环境文件中 -> APP_URL 设置为 localhost 并且 Dusk 试图访问 localhost:800/recipes。我通过断言完整的 URL 进行了检查

     $this->browse(function (Browser $browser) {
                $browser
                    ->loginAs(User::find(1))
                    ->visit('/recipes')
                    ->assertUrlIs('http://recipemanager.test/recipes');
            });
    
    Error: Actual URL [http://localhost/recipes] does not equal expected URL [http://recipemanager.test/recipes].
    

    .env文件

    APP_URL=http://localhost
    

    在我将 APP_URL 更改为 http://recipemanager.test 并访问 /recipes 后一切正常!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-16
      • 2021-06-19
      • 1970-01-01
      相关资源
      最近更新 更多