【问题标题】:Expected errors in Laravel DuskLaravel Dusk 中的预期错误
【发布时间】:2019-03-21 11:32:26
【问题描述】:

我正在寻找相当于 Laravel Dusk 的

this->expectException(InvalidArgumentException::class); 

我刚开始使用 Laravel Dusk(运行 Laravel 5.7),找不到测试预期错误的方法。当我运行以下测试时。

我得到以下信息:

有 1 个错误:

1) 测试\浏览器\注册测试::test_user_cannot_register_with_duplicate_email Illuminate\Database\QueryException: SQLSTATE[23000]: 完整性 约束违规:19 唯一约束失败:users.email(SQL: 插入“用户”(“名称”、“电子邮件”、“密码”、“updated_at”、 "created_a t") 值 (Eoj, joe@example.com, 654321, 2018-10-16 20:35:09, 2018-10-16 20:35:09))

由 PDOException 引起:SQLSTATE[23000]:完整性约束 违规:19 UNIQUE 约束失败:users.email

错误!测试:4,断言:5,错误:1。

public function test_user_cannot_register_with_duplicate_email()
{
    User::create([
        'name' => 'Eoj',
        'email' => 'joe@example.com',
        'password' => '654321'

    ]);

    $this->browse(function ($browser) {
        $browser->visit('/') //Go to the homepage
        ->clickLink('Register') //Click the Register link
        ->assertSee('Register') //Make sure the phrase in the argument is on the page
        //Fill the form with these values
        ->value('#name', 'Joe')
            ->value('#email', 'joe@example.com')
            ->value('#password', '123456')
            ->value('#password-confirm', '123456')
            ->click('button[type="submit"]') //Click the submit button on the page
            ->assertPathIs('/register') //Make sure you are still on the register page
            //Make sure you see the phrase in the argument
            ->assertSee("The email has already been taken");
    });

}

显然我期待一个错误 - 但无法弄清楚如何告诉黄昏这个。

当我将代码更改为以下代码时,我得到一个不同的错误:

1) >Tests\Browser\RegistrationTest::test_user_cannot_register_with_duplicate_email 没有在元素 [body] 中看到预期的文本 [电子邮件已被接收]。 断言 false 为 true 失败。

我目前没有运行任何其他测试。

    <?php

namespace Tests\Browser;

use App\User;
use Tests\DuskTestCase;
use Laravel\Dusk\Browser;
use Illuminate\Foundation\Testing\RefreshDatabase;


class RegistrationTest extends DuskTestCase

{
    use RefreshDatabase;

protected function setUp()
{
    parent::setUp();
    foreach (static::$browsers as $browser) {
        $browser->driver->manage()->deleteAllCookies();
    }
}

public function test_user_cannot_register_with_duplicate_email()
{
    User::create([
        'name' => 'Eoj',
        'email' => 'someone@example.com',
        'password' => '654321'

    ]);

    $this->browse(function ($browser) {
        $browser->visit('/') //Go to the homepage
        ->clickLink('Register') //Click the Register link
        ->assertSee('Register') //Make sure the phrase in the argument is on the page
        //Fill the form with these values
        ->value('#name', 'Joe')
            ->value('#email', 'someone@example.com')
            ->value('#password', '123456')
            ->value('#password-confirm', '123456')
            ->click('button[type="submit"]') //Click the submit button on the page
            ->assertPathIs('/register') //Make sure you are still on the register page
            //Make sure you see the phrase in the argument
            ->assertSee("The email has already been taken");
    });

}

}

【问题讨论】:

  • 看来您的注册用户代码有问题,而不是测试。您可能希望共享保存用户的控制器/模型代码。
  • 这是开箱即用的 Laravel Auth/RegisterController.php,您可以在运行 make:auth 后获得。当我在浏览器中对其进行测试时,它按预期工作(即当电子邮件已存在于数据库中时,不允许您注册用户)。我还应该按预期添加注册新唯一用户的测试。
  • 您的数据库中是否已有电子邮件joe@example.com?你可能需要使用RefreshDatabase trait。
  • 您的测试中的User::create([..]) 似乎是引发此错误的那个。将电子邮件更改为完全不同的电子邮件,然后重试。
  • 我不认为你明白我想说什么。您的测试正在尝试插入已重复的电子邮件。这就是为什么你有这个错误。在User::create-&gt;value('#email', '...') 中将此电子邮件更改为另一封电子邮件

标签: php laravel-5 laravel-dusk


【解决方案1】:

正如您在错误中看到的那样:

Tests\Browser\RegistrationTest::test_user_cannot_register_with_duplicate_email Illuminate\Database\QueryException:SQLSTATE[23000]:完整性约束违规:19 唯一约束失败:users.email(SQL:插入“users”(“name”,“email”) , "password", "updated_at", "created_at") 值 (Eoj, joe@example.com, 654321, 2018-10-16 20:35:09, 2018-10-16 20:35:09))

这些值正是您要在此处插入的值:

User::create([
    'name' => 'Eoj',
    'email' => 'joe@example.com',
    'password' => '654321'
]);

因此,您可以将此电子邮件更改为其他电子邮件:

User::create([
    'name' => 'Eoj',
    'email' => 'someone@example.com',
    'password' => '654321'
]);

这里再次测试是有道理的:

->value('#email', 'someone@example.com')

这样,电子邮件就不会重复,因为在某些时候您已经有了一个“joe@example.com”。

一个好的做法是在测试之前重置数据库。

tests/TestCase.php 你可能有这样的东西:

use Illuminate\Foundation\Testing\RefreshDatabase; //<--HERE

abstract class TestCase extends BaseTestCase
{
    use RefreshDatabase; //<--HERE

而且你的测试必须扩展这个类:

use Tests\TestCase; //<--HERE
class MyTest extends TestCase { //<--HERE

这样你的数据就被重置了,避免了后续测试中重复邮件的问题。

【讨论】:

  • 对不起,费利佩,但您完全误解了我正在运行的测试的全部意义。我正在尝试检查是否无法创建 DUPLICATE 用户 - 因此数据库中需要有重复的记录!我已经在使用 RefreshDatabase。
  • 你看过我发布的内容了吗?您已经在手动插入重复条目。你有没有按照我说的去做?
  • 按你说的做会创建一个新用户...这不是我想做的!
  • 这是一个关于如何在 Laravel Dusk 中预期错误的问题,类似于 this->expectException(InvalidArgumentException::class);在phpunit中
  • 让我们考虑一下。测试的重点是检查在尝试注册重复的电子邮件时,应用程序是否应返回“电子邮件已被接收”。因此,首先您手动注册电子邮件,然后强制进行第二次注册。关键是你的第一个寄存器 (create) 已经被复制了。这就是您需要更改此电子邮件的原因。
【解决方案2】:

我认为问题在于您没有等待服务器的回答(您是通过 js 提交表单,对吗?),并且断言触发得太早了。只需在这些类型的场景中使用waitForwaitForText

【讨论】:

    猜你喜欢
    • 2018-03-24
    • 2018-03-07
    • 2017-08-07
    • 1970-01-01
    • 2019-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-04
    相关资源
    最近更新 更多