좋아요 알림 테스트

김윤수·2023년 4월 27일
0

laravel

목록 보기
6/11

https://laravel-notification-channels.com/fcm/ 패키지를 통해 Firebase Cloud Messaging 알림 기능 구현후 테스트 코드 작성

알림을 어떻게 테스트해야 하나 난감했는데, Notification으로 Mocking(모의) 객체을 만들 수 있어서 구글링 통해 구현

모의객체에 알림을 보내고 해당 객체를 확인하는 구조

test('좋아요 알림 테스트 ', function (): void {
	Notification::fake();

    $user        = User::factory()->create();
    $likedByUser = User::factory()->create();
    $post        = Post::factory([
        'user_id' => $user->id
    ])->create();

    $post->user->notify(new PostLiked($post, $likedByUser));

	// 알림 받았는지. (내용까지 확인)
    Notification::assertSentTo(
        $user,
        PostLiked::class,
        function ($notification, $channels) use ($user) {
            $fcmData = $notification->toFcm($user);
            $this->assertStringContainsString("좋아요", $fcmData->getNotification()->getBody());
            return true;
        }
    );
    
    // 알림 안받았는지
    Notification::assertNotSentTo(
        $likedByUser,
        PostLiked::class
    );
});
profile
안녕하세요

0개의 댓글