Base solution for your next web application
Open Closed

Testing Publish and Notification #2509


User avatar
0
drsmallbone created

Hi,

I am having an issue with testing if a notification is received. I have set up a unit test to but my test never completes, when I debug the test when it hits my publish method it just ends. I am passing a list of of users so it should not be anything to do with subscriptions.

My Test

public class Publisher_Tests : FooTestBase
    {
        private readonly IPublisher _notificationPublisher;
        private readonly IUserNotificationManager _userNotificationManager;

        public Publisher_Tests()
        {
            _notificationPublisher = Resolve<IPublisher>();
            _userNotificationManager = Resolve<IUserNotificationManager>();
        }

        [Fact]
        public void Publish_ShouldPublishGeneralNotification()
        {
            string notificationName = "TestGeneralNotification";
            UserIdentifier userIndentifier = new UserIdentifier(1, 2);
            List<UserIdentifier> userIdentifiers = new List<UserIdentifier>
            {
                userIndentifier
            };
            _notificationPublisher.Publish(notificationName, userIdentifiers);

            UsingDbContext(context =>
            {
                var output = _userNotificationManager.GetUserNotificationCount(userIndentifier);
                output.ShouldBeGreaterThan(0);
            });
        }
    }

My Method

public class Publisher : DomainService, IPublisher
    {
        private readonly INotificationPublisher _notificationPublisher;

        public Publisher(INotificationPublisher notificationPublisher)
        {
            _notificationPublisher = notificationPublisher;
        }

        public void Publish(string notificationName, List<UserIdentifier> userIdentifiers)
        {
            _notificationPublisher.Publish(notificationName, null, null, NotificationSeverity.Info, userIdentifiers.ToArray());
        }

        public async Task PublishAsync(string notificationName, List<UserIdentifier> userIdentifiers)
        {
            await _notificationPublisher.PublishAsync(notificationName, null, null, NotificationSeverity.Info, userIdentifiers.ToArray());
        }
    }

Any help would be really appreciated.


3 Answer(s)
  • User Avatar
    0
    chrisk created

    Hi,

    I believe that you'll need to to use INotificationDistributer to distribute notifications and then assert your test. Check this [https://github.com/aspnetboilerplate/aspnetboilerplate/blob/11f2a25119c0447b0581172cd6aa9c3ee67d916f/src/Abp/Notifications/NotificationDistributer.cs]) So when you publish notification it's going to be saved as TenantNotification and then publisher saves them as UserNotifications during actual publish.

  • User Avatar
    0
    drsmallbone created

    Thanks Chrisk,

    I know the underlying NotificationPublisher uses BackgroundJobs, as well as the NotificationDistributer but not sure how I can test my interface as it will not mock the underlying class.

    Thanks for your help

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @drsmallbone,

    Thank you for sharing your code.

    @Chrisk, than you for your anser.

    When you publish a notification less then 5 people (see <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/f10fa5205c780bcc27adfe38aaae631f412eb7df/src/Abp/Notifications/NotificationPublisher.cs#L20">https://github.com/aspnetboilerplate/as ... her.cs#L20</a>), it should be distributed automatically.

    Let us test this and get back to you.

    Thanks.