From 127652002b96a525c71ef8e3336bcc73ecf87c06 Mon Sep 17 00:00:00 2001 From: Denis Nutiu Date: Sun, 18 Jul 2021 13:05:32 +0300 Subject: [PATCH] Add unit tests for FeedbacksService.GetFeedbacksAsync --- .../Services/FeedbacksService.cs | 1 - .../Services/TestFeedbacksService.cs | 105 ++++++++++++++++++ 2 files changed, 105 insertions(+), 1 deletion(-) diff --git a/Retroactiune.Core/Services/FeedbacksService.cs b/Retroactiune.Core/Services/FeedbacksService.cs index 4c6630f..db4a65d 100644 --- a/Retroactiune.Core/Services/FeedbacksService.cs +++ b/Retroactiune.Core/Services/FeedbacksService.cs @@ -28,7 +28,6 @@ namespace Retroactiune.Core.Services public async Task> GetFeedbacksAsync(FeedbacksListFilters filters) { - // TODO: Unit test. Guard.Against.Null(filters, nameof(filters)); Guard.Against.Null(filters.FeedbackReceiverId, nameof(filters.FeedbackReceiverId)); diff --git a/Retroactiune.UnitTests/Retroactiune.Core/Services/TestFeedbacksService.cs b/Retroactiune.UnitTests/Retroactiune.Core/Services/TestFeedbacksService.cs index ba69681..bb2d2e3 100644 --- a/Retroactiune.UnitTests/Retroactiune.Core/Services/TestFeedbacksService.cs +++ b/Retroactiune.UnitTests/Retroactiune.Core/Services/TestFeedbacksService.cs @@ -81,5 +81,110 @@ namespace Retroactiune.Tests.Retroactiune.Core.Services It.IsAny(), It.IsAny())); } + + [Fact] + public async Task Test_GetFeedbacksAsync_NullGuards() + { + // Setup + var mongoDatabaseMock = new Mock(); + var mongoClientMock = new Mock(); + var mongoSettingsMock = new Mock(); + var mongoCollectionMock = new Mock>(); + + mongoSettingsMock.SetupGet(i => i.DatabaseName).Returns("MyDB"); + mongoSettingsMock.SetupGet(i => i.FeedbacksCollectionName).Returns("feedbacks"); + + mongoClientMock + .Setup(stub => stub.GetDatabase(It.IsAny(), + It.IsAny())) + .Returns(mongoDatabaseMock.Object); + + mongoDatabaseMock + .Setup(i => i.GetCollection(It.IsAny(), + It.IsAny())) + .Returns(mongoCollectionMock.Object); + + // Test & Assert + var service = new FeedbacksService(mongoClientMock.Object, mongoSettingsMock.Object); + await Assert.ThrowsAsync(async () => { await service.GetFeedbacksAsync(null); }); + await Assert.ThrowsAsync(async () => + { + await service.GetFeedbacksAsync(new FeedbacksListFilters()); + }); + } + + + [Theory, AutoData] + public async Task Test_GetFeedbacksAsync_Happy(FeedbacksListFilters feedbacksListFilters) + { + // Setup + var mongoDatabaseMock = new Mock(); + var mongoClientMock = new Mock(); + var mongoSettingsMock = new Mock(); + var mongoCollectionMock = new Mock>(); + + mongoSettingsMock.SetupGet(i => i.DatabaseName).Returns("MyDB"); + mongoSettingsMock.SetupGet(i => i.FeedbacksCollectionName).Returns("feedbacks"); + + mongoClientMock + .Setup(stub => stub.GetDatabase(It.IsAny(), + It.IsAny())) + .Returns(mongoDatabaseMock.Object); + + mongoDatabaseMock + .Setup(i => i.GetCollection(It.IsAny(), + It.IsAny())) + .Returns(mongoCollectionMock.Object); + + mongoCollectionMock.Setup(i => i.FindAsync(It.IsAny>(), + It.IsAny>(), It.IsAny())) + .ReturnsAsync(new Mock>().Object); + + // Test + var service = new FeedbacksService(mongoClientMock.Object, mongoSettingsMock.Object); + await service.GetFeedbacksAsync(feedbacksListFilters); + + // Assert + mongoCollectionMock.Verify(i => i.FindAsync(It.IsAny>(), + It.IsAny>(), It.IsAny())); + } + + [Theory, AutoData] + public async Task Test_GetFeedbacksAsync_Happy_MinimalFilters(string feedbackReceiverId) + { + // Setup + var mongoDatabaseMock = new Mock(); + var mongoClientMock = new Mock(); + var mongoSettingsMock = new Mock(); + var mongoCollectionMock = new Mock>(); + + mongoSettingsMock.SetupGet(i => i.DatabaseName).Returns("MyDB"); + mongoSettingsMock.SetupGet(i => i.FeedbacksCollectionName).Returns("feedbacks"); + + mongoClientMock + .Setup(stub => stub.GetDatabase(It.IsAny(), + It.IsAny())) + .Returns(mongoDatabaseMock.Object); + + mongoDatabaseMock + .Setup(i => i.GetCollection(It.IsAny(), + It.IsAny())) + .Returns(mongoCollectionMock.Object); + + mongoCollectionMock.Setup(i => i.FindAsync(It.IsAny>(), + It.IsAny>(), It.IsAny())) + .ReturnsAsync(new Mock>().Object); + + // Test + var service = new FeedbacksService(mongoClientMock.Object, mongoSettingsMock.Object); + await service.GetFeedbacksAsync(new FeedbacksListFilters() + { + FeedbackReceiverId = feedbackReceiverId + }); + + // Assert + mongoCollectionMock.Verify(i => i.FindAsync(It.IsAny>(), + It.IsAny>(), It.IsAny())); + } } } \ No newline at end of file