跳到主要内容
版本:3.x

测试

概述

All examples in this guide will be written using Pest. However, you can easily adapt this to 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.'),
);