【发布时间】: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?你可能需要使用RefreshDatabasetrait。 -
您的测试中的
User::create([..])似乎是引发此错误的那个。将电子邮件更改为完全不同的电子邮件,然后重试。 -
我不认为你明白我想说什么。您的测试正在尝试插入已重复的电子邮件。这就是为什么你有这个错误。在
User::create和->value('#email', '...')中将此电子邮件更改为另一封电子邮件
标签: php laravel-5 laravel-dusk