我对Laravel Dusk还是很陌生(比如不到24小时),我正在尝试创建一些测试,但我无法克服最初的测试。
所以我有UserCanRegisterTest.php
和UserCanSeeDashboardTest.php
,在UserCanRegisterTest.php
中注册一个用户,如何在UserCanSeeDashboardTest.php
中访问该用户信息而不必重新创建另一个用户?我曾尝试进行研究,但跌倒了一个小洞,研究了内存,cookie,DatabaseTransactions,但似乎没有任何意义或显示示例。
我可以使用$faker->safeEmail
中$password
中的UserCanRegisterTest.php
和UserCanSeeDashboardTest.php
以及我进行的所有其他测试吗?
UserCanRegisterTest.php:
<?php
namespace Tests\Browser;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Laravel\Dusk\Browser;
use Tests\DuskTestCase;
class UserCanRegisterTest extends DuskTestCase
{
use DatabaseMigrations;
/*public function setUp()
{
parent::setUp();
$this->artisan('db:seed');
}*/
/** @test */
public function user_passes_registration_form()
{
$faker = \Faker\Factory::create();
/*$roleSeeder = new RoleTableSeeder();
$roleSeeder->run();
$permissionSeeder = new PermissionTableSeeder();
$permissionSeeder->run();*/
$this->browse(function($browser) use ($faker) {
$password = $faker->password(9);
$browser->visit('/register')
//->assertSee('Welcome Back!')
->type('company_name', $faker->company)
->type('name', $faker->name)
->type('email', $faker->safeEmail)
->type('password', $password)
->type('password_confirmation', $password)
->press('REGISTER')
->assertPathIs('/register');
});
}
}
这里是UserCanSeeDashboardTest.php ((请注意我想如何使用上述测试中的$ faker-> safeEmail和$ password,因此我不需要每次都创建新用户)。
<?php
namespace Tests\Browser;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Laravel\Dusk\Browser;
use Tests\DuskTestCase;
use App\User;
class UserCanSeeDashboardTest extends DuskTestCase
{
use DatabaseMigrations;
/*public function setUp()
{
parent::setUp();
//$this->artisan('db:seed');
}*/
/** @test */
public function test_I_can_login_successfully()
{
$this->browse(function ($browser) {
//$user->roles()->attach(1); //Attach user admin role
$browser->visit('/login')
->type('email', $faker->safeEmail)
->type('password', $password)
->press('SIGN IN')
->assertSee('Dashboard');
});
}
}
理想情况下,我有一个注册用户的测试,然后有其他测试使用该注册用户的数据登录并测试应用程序的其他部分。
PHPUnit对相互依赖的测试没有很好的支持。大多数情况下,PHPUnit中的测试应视为独立的。框架确实提供了@depends
批注,您可能已经可以将其用于依赖于注册方法的测试,但是它仅适用于相同@depends
中的测试。
相反,我建议您保持注册测试不变,并使用特征或继承来添加一些方法,您可以重复使用这些方法来从其他测试方法注册或登录。
您可以创建一个从class继承并包含注册测试用户的方法的类MyDuskTestCase
:
DuskTestCase
您可以在<?php
namespace Tests;
use Tests\DuskTestCase;
use App\User;
use Hash;
abstract class MyDuskTestCase extends DuskTestCase
{
private $email = '[email protected]';
private $password = 'password';
public function setup(): void
{
parent::setUp();
// If you want to run registerTestUser for every test:
// registerTestUser();
}
public function registerTestUser()
{
// For simple cases
return User::create([
'email' => $this->email,
'name' => 'My name',
'password' => Hash::make($this->password)
]);
// Otherwise, register the user through your app (slower)
$this->browse(function($browser) use ($faker) {
$password = $faker->password(9);
$browser->visit('/register')
->type('company_name', $faker->company)
->type('name', $faker->name)
->type('email', $this->email)
->type('password', $this->password)
->type('password_confirmation', $this->password)
->press('REGISTER');
});
}
public function getTestUser()
{
return User::where('email', $this->email)->first();
}
}
方法中运行registerTestUser
方法来为每个测试创建测试用户,也可以从需要用户的测试中调用该方法。例如:
setup
对于登录,您可以在基本测试类中添加其他方法以登录测试用户,也可以使用Dusk提供的<?php
namespace Tests\Browser;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Laravel\Dusk\Browser;
use Tests\MyDuskTestCase;
class UserCanRegisterTest extends MyDuskTestCase
{
use DatabaseMigrations;
public function test_I_can_login_successfully()
{
$this->registerTestUser();
$this->browse(function ($browser) use ($user) {
$browser->visit('/login')
->type('email', $this->email)
->type('password', $this->password)
->press('SIGN IN')
->assertSee('Dashboard');
});
}
}
方法:
loginAs