kmnurunnabi's avatar

Help me writing Test

I have a controller method:

public function show(string $id)
    {
        $notification = auth()->user()->notifications()->findOrFail($id);

        if (is_null($notification->read_at)) {
            $notification->markAsRead();
        }

        return to_route('posts.show', $notification->data['post_id']);
    }

and the route is route('notifications.show', $notificationId). Can anyone suggest me how can I write a feature test for this controller method?

0 likes
9 replies
nexxai's avatar

Why not something like this

it('can show the notifications', function() {
	$notification = Notification::factory()->create();

	$this->get(route('notifications.show', $notification->id))
		->assertRedirect(route('posts.show'));

	$this->assertNotNull($notification->fresh()->read_at);
});
kmnurunnabi's avatar

@nexxai But one more problem. Notification can not have it's model when you can access it via auth()->user()->notifications. Then what can be the solution for this?

nexxai's avatar

@kmnurunnabi So you'll need to create a user and create a notification attached to that user.

Please or to participate in this conversation.