公告: 旧版文档请移步 -> http://v2.laravel-filament.cn/docs

Languages

Version

Theme

表单构造器

测试

所有本指南内的示例都会使用 Pest 编写。当然,你也可以使用 PHPUnit。

因为表单构造器是基于 Livewire 组件的,因此你也可以使用 Livewire 测试辅助函数。不过,我们自定义了一些测试辅助函数,让你可以用于表单测试:

填充表单

要填充表单,请将数据传入 fillform()

use function Pest\Livewire\livewire;
 
livewire(CreatePost::class)
->fillForm([
'title' => fake()->sentence(),
// ...
]);

注意,如果一个 Livewire 组件中有多个表单,你可以使用 fillForm([...], 'createPostForm') 指定你想要填充哪个表单。

使用 assertFormSet() 检测表单是否有数据:

use Illuminate\Support\Str;
use function Pest\Livewire\livewire;
 
it('can automatically generate a slug from the title', function () {
$title = fake()->sentence();
 
livewire(CreatePost::class)
->fillForm([
'title' => $title,
])
->assertFormSet([
'slug' => Str::slug($title),
]);
});

注意,如果一个 Livewire 组件中有多个表单,你可以使用 assertFormSet([...], 'createPostForm') 指定你想要检测哪个表单。

验证

使用 assertHasFormErrors() 断言表单中的数据是否有效验证:

use function Pest\Livewire\livewire;
 
it('can validate input', function () {
livewire(CreatePost::class)
->fillForm([
'title' => null,
])
->assertHasFormErrors(['title' => 'required']);
});

同时 assertHasNoFormErrors() 断言没有验证错误:

use function Pest\Livewire\livewire;
 
livewire(CreatePost::class)
->fillForm([
'title' => fake()->sentence(),
// ...
])
->call('save')
->assertHasNoFormErrors();

注意,如果一个 Livewire 组件中有多个表单,你可以将指定表单的名字作为第二个参数,像这样 assertHasFormErrors(['title' => 'required'], 'createPostForm')assertHasNoFormErrors([], 'createPostForm') 传入。

表单存在

断言 Livewire 组件中有一个表单,请使用 assertFormExists():

use function Pest\Livewire\livewire;
 
it('has a form', function () {
livewire(CreatePost::class)
->assertFormExists();
});

注意,如果一个 Livewire 组件中有多个表单,你可以将指定的表单名传入 assertFormExists('createPostForm').

字段

断言表单中有某个给定字段,将字段名传入到 assertFormFieldExists()

use function Pest\Livewire\livewire;
 
it('has a title field', function () {
livewire(CreatePost::class)
->assertFormFieldExists('title');
});

你也可以传入函数作为额外的参数,来断言一个字段传入了一个“真值测试”。这对断言一个字段有某个特定配置非常有用:

use function Pest\Livewire\livewire;
 
it('has a title field', function () {
livewire(CreatePost::class)
->assertFormFieldExists('title', function (TextInput $field): bool {
return $input->isDisabled();
});
});

注意,如果你的 Livewire 组件有多个表单,你可以使用 assertFormFieldExists('title', 'createPostForm') 指定哪个表单,要检测字段是否存在。

Hidden 字段

断言表单中有某个字段可见,将字段名传入到 assertFormFieldIsVisible():

use function Pest\Livewire\livewire;
 
test('title is visible', function () {
livewire(CreatePost::class)
->assertFormFieldIsVisible('title');
});

或者断言表单中隐藏某个字段,将字段名传入到 assertFormFieldIsHidden():

use function Pest\Livewire\livewire;
 
test('title is hidden', function () {
livewire(CreatePost::class)
->assertFormFieldIsHidden('title');
});

Note that for both assertFormFieldIsHidden() and assertFormFieldIsVisible() you can pass the name of a specific form the field belongs to as the second argument like assertFormFieldIsHidden('title', 'createPostForm').

Disabled 字段

断言字段 enabled,将字段名传入到 assertFormFieldIsEnabled():

use function Pest\Livewire\livewire;
 
test('title is enabled', function () {
livewire(CreatePost::class)
->assertFormFieldIsEnabled('title');
});

或者断言字段 disabled,将字段名传入到 assertFormFieldIsDisabled():

use function Pest\Livewire\livewire;
 
test('title is disabled', function () {
livewire(CreatePost::class)
->assertFormFieldIsDisabled('title');
});

Note that for both assertFormFieldIsEnabled() and assertFormFieldIsDisabled() you can pass the name of a specific form the field belongs to as the second argument like assertFormFieldIsEnabled('title', 'createPostForm').

Edit on GitHub

Still need help? Join our Discord community or open a GitHub discussion

上一页
高级