Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

.NetCore: Unit Test GetUserNotifications returns 0 notifications #5707


User avatar
0
klir created

Hi,

I'm adding some tests for a global notification process. Basically I've registered an EntityCreatedEventData in the evenbus and every time a record is created a notification is published. All works ok while debugging the app. But now I want to lock this behaviour by adding some unit test that replicate this logic. However I couldn't figure out how to make this work. In the unit test below, a new notification is publised, current users is subscribed, Created Event is raised, but notificationSubscriptionManager returns always 0. Can you help me with this one?

public NotificationAppService_Tests()
{
    _notificationAppService = Resolve<INotificationAppService>();
    _userNotificationManager = Resolve<IUserNotificationManager>();
    _notificationSubscriptionManager = Resolve<INotificationSubscriptionManager>();
    _taskRepository = Resolve<IRepository<TaskEntity, long>>();
}


...


[Fact]
public async Task User_Receives_NewTaskCreated_Notification()
{
    //Subscribe current user to NewTaskCreated 
    var user = await GetCurrentUserAsync();
    await _notificationSubscriptionManager.SubscribeAsync(user.ToUserIdentifier(), AppNotificationNames.NewTaskCreated);

    //Create a new task:
    long taskId = 0;
    Action action = () =>
    {
        using (var uow = Resolve<IUnitOfWorkManager>().Begin())
        {
            taskId = _taskRepository.InsertAndGetId(new TaskEntity
            {
                Name = "New task for subscribers!!",
                CreatorUserId = AbpSession.UserId,
                LastModifierUserId = AbpSession.UserId,
                TenantId = AbpSession.TenantId,
                Code = "01",
                Description = "New task for subscribers!!",
                OwnerUserId = AbpSession.UserId,
                PermitId = 1, //Should be present in
            });


            var newTaskCreated = _taskRepository.FirstOrDefault(taskId);
            newTaskCreated.ShouldNotBeNull();

            //// Notify subscribers <== this happen in the EntityCreatedEventData once uow.Complerted()
            //// await _appNotifier.NewTaskCreatedAsync(newTaskCreated);

            uow.Complete();

        }
    };
    action
        .ShouldNotThrow();

    var userSubscriptions = _notificationSubscriptionManager
        .GetSubscribedNotifications(user.ToUserIdentifier());
    userSubscriptions
        .Count
        .ShouldBeGreaterThan(0);

    //User should receive notification
    var notifications = _userNotificationManager.GetUserNotifications(
        user.ToUserIdentifier()
    );

    //Asserts
    notifications
        .Count
        .ShouldBeGreaterThan(0); // <== **FAILS HERE retrieves 0 notifications!!!**


    var item = notifications.First();
    item.Notification
        .NotificationName
        .ShouldBe(AppNotificationNames.NewTaskCreated);

}

2 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    Distribution is enqueued as background job in NotificationPublisher.

    You can subclass BackgroundJobManager and set JobPollPeriod to a very small TimeSpan.

  • User Avatar
    0
    klir created

    That's great.. But I prefer not to having to wait 5 seconds or 100ms.. I think it's just ok for now if I intercept that call. That should be sufficient to pass the test.  Thanks

    //Mock Background Job an intercept queue
    NotificationDistributionJobArgs calledNotification = null;
    var fakeBackgroundJobManager = Substitute.For<IBackgroundJobManager>();
    fakeBackgroundJobManager.EnqueueAsync<NotificationDistributionJob, NotificationDistributionJobArgs>(
        Arg.Any<NotificationDistributionJobArgs>(),
        Arg.Any<BackgroundJobPriority>()).Returns(callInfo =>
        {
            calledNotification = callInfo.Arg<NotificationDistributionJobArgs>();
            return Task.CompletedTask;
        });
    LocalIocManager.IocContainer.Register(Component.For<IBackgroundJobManager>().Instance(fakeBackgroundJobManager).IsDefault());
    
    ...
    
    //Asserts
        calledNotification
            .ShouldNotBeNull(); //