我正在寻找与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 UNIQUE约束失败: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 在元素[正文]中未看到预期的文本[电子邮件已被接收]。 无法断言false为真。
此刻我没有进行任何其他测试。
<?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");
});
}
}
答案 0 :(得分:1)
您会在错误中看到:
Tests \ Browser \ RegistrationTest :: test_user_cannot_register_with_duplicate_email Illuminate \ Database \ QueryException:SQLSTATE [23000]:违反完整性约束:19 UNIQUE约束失败: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))< / p>
这些值正是您要在此处插入的值:
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
这样,您就可以重置数据,避免在后续测试中出现重复的电子邮件问题。
答案 1 :(得分:0)
我认为问题在于您没有等待服务器的答复(您正在通过js提交表单,对吗?),并且断言触发得太早了。在这种情况下,只需使用waitFor
或waitForText
。