Laravel Livewire 测试
Laravel Livewire testing
我也很期待学习 Livewire 及其测试。现在我已经创建了用于注册用户的组件并且工作正常,但是当我尝试进行测试时出现此错误:
Failed asserting that an array has the key 'redirect'.
以下是部分代码:
注册测试
class RegisterTest extends TestCase
{
use RefreshDatabase;
/** @test */
public function can_register()
{
Livewire::test('auth.register')
->set('name', 'user')
->set('email', 'user@outlook.com')
->set('password', 'secret')
->set('password_confirmation', 'secret')
->call('register')
->assertRedirect('/');
}
}
组件
public $name = '';
public $email = '';
public $password = '';
public $password_confirmation = '';
public function register()
{
$data = $this->validate([
'name' => 'required|string',
'email' => 'required|email',
'password' => 'required|confirmed|min:8|string'
]);
User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
return redirect('/');
}
这很简单。
添加以下文件:
->assertHasNoErrors(['name', 'email', 'password']);
之前
->assertRedirect('/');
行,你会发现它会失败。这是因为您的验证告诉密码应该至少 8 个字符,而在您的测试中它是 6 个(您使用 secret
作为密码)
我也很期待学习 Livewire 及其测试。现在我已经创建了用于注册用户的组件并且工作正常,但是当我尝试进行测试时出现此错误:
Failed asserting that an array has the key 'redirect'.
以下是部分代码:
注册测试
class RegisterTest extends TestCase
{
use RefreshDatabase;
/** @test */
public function can_register()
{
Livewire::test('auth.register')
->set('name', 'user')
->set('email', 'user@outlook.com')
->set('password', 'secret')
->set('password_confirmation', 'secret')
->call('register')
->assertRedirect('/');
}
}
组件
public $name = '';
public $email = '';
public $password = '';
public $password_confirmation = '';
public function register()
{
$data = $this->validate([
'name' => 'required|string',
'email' => 'required|email',
'password' => 'required|confirmed|min:8|string'
]);
User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
return redirect('/');
}
这很简单。
添加以下文件:
->assertHasNoErrors(['name', 'email', 'password']);
之前
->assertRedirect('/');
行,你会发现它会失败。这是因为您的验证告诉密码应该至少 8 个字符,而在您的测试中它是 6 个(您使用 secret
作为密码)