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

Languages

Version

Theme

通知生成器

测试

概述

所有本指南内的示例都使用 Pest 编写。要使用 Pest 的 Livewire 插件进行测试,请参考 Pest 文档中的插件安装说明:Pest 的 Livewire 插件。不过,你也可以将其适配到 PHPUnit。

测试 session 通知

要检查通知是否通过 Session 发送,请使用 assertNotified() 辅助函数:

use function Pest\Livewire\livewire;
 
it('sends a notification', function () {
livewire(CreatePost::class)
->assertNotified();
});
use Filament\Notifications\Notification;
 
it('sends a notification', function () {
Notification::assertNotified();
});
use function Filament\Notifications\Testing\assertNotified;
 
it('sends a notification', function () {
assertNotified();
});

可选地,你可以传入一个通知标题去测试:

use Filament\Notifications\Notification;
use function Pest\Livewire\livewire;
 
it('sends a notification', function () {
livewire(CreatePost::class)
->assertNotified('Unable to create post');
});

或者测试某个特定通知是否准确发送:

use Filament\Notifications\Notification;
use function Pest\Livewire\livewire;
 
it('sends a notification', function () {
livewire(CreatePost::class)
->assertNotified(
Notification::make()
->danger()
->title('Unable to create post')
->body('Something went wrong.'),
);
});

与之相反,你也可以断言通知未发生:

use Filament\Notifications\Notification;
use function Pest\Livewire\livewire;
 
it('does not send a notification', function () {
livewire(CreatePost::class)
->assertNotNotified()
// or
->assertNotNotified('Unable to create post')
// or
->assertNotified(
Notification::make()
->danger()
->title('Unable to create post')
->body('Something went wrong.'),
);
Edit on GitHub

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