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?
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);
});
@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?
@kmnurunnabi Do you have the relationship setup between Users and Notifications?
@nexxai . I have notifiable traits declared inside User model.
@kmnurunnabi So you'll need to create a user and create a notification attached to that user.
Please sign in or create an account to participate in this conversation.