Add unit tests for FeedbacksService.GetFeedbacksAsync

This commit is contained in:
Denis-Cosmin Nutiu 2021-07-18 13:05:32 +03:00
parent cf10df84f2
commit 127652002b
2 changed files with 105 additions and 1 deletions

View file

@ -28,7 +28,6 @@ namespace Retroactiune.Core.Services
public async Task<IEnumerable<Feedback>> GetFeedbacksAsync(FeedbacksListFilters filters) public async Task<IEnumerable<Feedback>> GetFeedbacksAsync(FeedbacksListFilters filters)
{ {
// TODO: Unit test.
Guard.Against.Null(filters, nameof(filters)); Guard.Against.Null(filters, nameof(filters));
Guard.Against.Null(filters.FeedbackReceiverId, nameof(filters.FeedbackReceiverId)); Guard.Against.Null(filters.FeedbackReceiverId, nameof(filters.FeedbackReceiverId));

View file

@ -81,5 +81,110 @@ namespace Retroactiune.Tests.Retroactiune.Core.Services
It.IsAny<InsertOneOptions>(), It.IsAny<InsertOneOptions>(),
It.IsAny<CancellationToken>())); It.IsAny<CancellationToken>()));
} }
[Fact]
public async Task Test_GetFeedbacksAsync_NullGuards()
{
// Setup
var mongoDatabaseMock = new Mock<IMongoDatabase>();
var mongoClientMock = new Mock<IMongoClient>();
var mongoSettingsMock = new Mock<IDatabaseSettings>();
var mongoCollectionMock = new Mock<IMongoCollection<Feedback>>();
mongoSettingsMock.SetupGet(i => i.DatabaseName).Returns("MyDB");
mongoSettingsMock.SetupGet(i => i.FeedbacksCollectionName).Returns("feedbacks");
mongoClientMock
.Setup(stub => stub.GetDatabase(It.IsAny<string>(),
It.IsAny<MongoDatabaseSettings>()))
.Returns(mongoDatabaseMock.Object);
mongoDatabaseMock
.Setup(i => i.GetCollection<Feedback>(It.IsAny<string>(),
It.IsAny<MongoCollectionSettings>()))
.Returns(mongoCollectionMock.Object);
// Test & Assert
var service = new FeedbacksService(mongoClientMock.Object, mongoSettingsMock.Object);
await Assert.ThrowsAsync<ArgumentNullException>(async () => { await service.GetFeedbacksAsync(null); });
await Assert.ThrowsAsync<ArgumentNullException>(async () =>
{
await service.GetFeedbacksAsync(new FeedbacksListFilters());
});
}
[Theory, AutoData]
public async Task Test_GetFeedbacksAsync_Happy(FeedbacksListFilters feedbacksListFilters)
{
// Setup
var mongoDatabaseMock = new Mock<IMongoDatabase>();
var mongoClientMock = new Mock<IMongoClient>();
var mongoSettingsMock = new Mock<IDatabaseSettings>();
var mongoCollectionMock = new Mock<IMongoCollection<Feedback>>();
mongoSettingsMock.SetupGet(i => i.DatabaseName).Returns("MyDB");
mongoSettingsMock.SetupGet(i => i.FeedbacksCollectionName).Returns("feedbacks");
mongoClientMock
.Setup(stub => stub.GetDatabase(It.IsAny<string>(),
It.IsAny<MongoDatabaseSettings>()))
.Returns(mongoDatabaseMock.Object);
mongoDatabaseMock
.Setup(i => i.GetCollection<Feedback>(It.IsAny<string>(),
It.IsAny<MongoCollectionSettings>()))
.Returns(mongoCollectionMock.Object);
mongoCollectionMock.Setup(i => i.FindAsync(It.IsAny<FilterDefinition<Feedback>>(),
It.IsAny<FindOptions<Feedback, Feedback>>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new Mock<IAsyncCursor<Feedback>>().Object);
// Test
var service = new FeedbacksService(mongoClientMock.Object, mongoSettingsMock.Object);
await service.GetFeedbacksAsync(feedbacksListFilters);
// Assert
mongoCollectionMock.Verify(i => i.FindAsync(It.IsAny<FilterDefinition<Feedback>>(),
It.IsAny<FindOptions<Feedback, Feedback>>(), It.IsAny<CancellationToken>()));
}
[Theory, AutoData]
public async Task Test_GetFeedbacksAsync_Happy_MinimalFilters(string feedbackReceiverId)
{
// Setup
var mongoDatabaseMock = new Mock<IMongoDatabase>();
var mongoClientMock = new Mock<IMongoClient>();
var mongoSettingsMock = new Mock<IDatabaseSettings>();
var mongoCollectionMock = new Mock<IMongoCollection<Feedback>>();
mongoSettingsMock.SetupGet(i => i.DatabaseName).Returns("MyDB");
mongoSettingsMock.SetupGet(i => i.FeedbacksCollectionName).Returns("feedbacks");
mongoClientMock
.Setup(stub => stub.GetDatabase(It.IsAny<string>(),
It.IsAny<MongoDatabaseSettings>()))
.Returns(mongoDatabaseMock.Object);
mongoDatabaseMock
.Setup(i => i.GetCollection<Feedback>(It.IsAny<string>(),
It.IsAny<MongoCollectionSettings>()))
.Returns(mongoCollectionMock.Object);
mongoCollectionMock.Setup(i => i.FindAsync(It.IsAny<FilterDefinition<Feedback>>(),
It.IsAny<FindOptions<Feedback, Feedback>>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new Mock<IAsyncCursor<Feedback>>().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<FilterDefinition<Feedback>>(),
It.IsAny<FindOptions<Feedback, Feedback>>(), It.IsAny<CancellationToken>()));
}
} }
} }