Add FeedbackReceiverService unit tests
This commit is contained in:
parent
bb23e6193e
commit
035d6d177a
3 changed files with 133 additions and 4 deletions
|
@ -1,6 +1,126 @@
|
|||
namespace Retroactiune.Tests.Retroactiune.WebAPI.Services
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using AutoFixture.Xunit2;
|
||||
using MongoDB.Driver;
|
||||
using Moq;
|
||||
using Retroactiune.Models;
|
||||
using Retroactiune.Services;
|
||||
using Retroactiune.Settings;
|
||||
using Xunit;
|
||||
|
||||
namespace Retroactiune.Tests.Retroactiune.WebAPI.Services
|
||||
{
|
||||
public class TestFeedbackReceiverService
|
||||
{
|
||||
[Theory, AutoData]
|
||||
public async Task Test_CreateManyAsync_Success(IEnumerable<FeedbackReceiver> items)
|
||||
{
|
||||
// Arrange
|
||||
var mongoDatabaseMock = new Mock<IMongoDatabase>();
|
||||
var mongoClientMock = new Mock<IMongoClient>();
|
||||
var mongoSettingsMock = new Mock<IMongoDbSettings>();
|
||||
var mongoCollectionMock = new Mock<IMongoCollection<FeedbackReceiver>>();
|
||||
|
||||
mongoSettingsMock.SetupGet(i => i.DatabaseName).Returns("MyDB");
|
||||
mongoSettingsMock.SetupGet(i => i.FeedbackReceiverCollectionName).Returns("feedback_receiver");
|
||||
|
||||
mongoClientMock
|
||||
.Setup(stub => stub.GetDatabase(It.IsAny<string>(), It.IsAny<MongoDatabaseSettings>()))
|
||||
.Returns(mongoDatabaseMock.Object);
|
||||
|
||||
mongoDatabaseMock
|
||||
.Setup(i => i.GetCollection<FeedbackReceiver>(It.IsAny<string>(), It.IsAny<MongoCollectionSettings>()))
|
||||
.Returns(mongoCollectionMock.Object);
|
||||
|
||||
// Test
|
||||
var service = new FeedbackReceiverService(mongoClientMock.Object, mongoSettingsMock.Object);
|
||||
await service.CreateManyAsync(items);
|
||||
|
||||
// Assert
|
||||
mongoClientMock.Verify(i => i.GetDatabase("MyDB", null), Times.Once());
|
||||
mongoDatabaseMock.Verify(
|
||||
i => i.GetCollection<FeedbackReceiver>("feedback_receiver", It.IsAny<MongoCollectionSettings>()),
|
||||
Times.Once());
|
||||
mongoCollectionMock.Verify(
|
||||
i => i.InsertManyAsync(items, It.IsAny<InsertManyOptions>(), It.IsAny<CancellationToken>()),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Test_CreateManyAsync_NullVal()
|
||||
{
|
||||
// Arrange
|
||||
var mongoDatabaseMock = new Mock<IMongoDatabase>();
|
||||
var mongoClientMock = new Mock<IMongoClient>();
|
||||
var mongoSettingsMock = new Mock<IMongoDbSettings>();
|
||||
var mongoCollectionMock = new Mock<IMongoCollection<FeedbackReceiver>>();
|
||||
|
||||
mongoSettingsMock.SetupGet(i => i.DatabaseName).Returns("MyDB");
|
||||
mongoSettingsMock.SetupGet(i => i.FeedbackReceiverCollectionName).Returns("feedback_receiver");
|
||||
|
||||
mongoClientMock
|
||||
.Setup(i => i.GetDatabase(It.IsAny<string>(), It.IsAny<MongoDatabaseSettings>()))
|
||||
.Returns(mongoDatabaseMock.Object);
|
||||
|
||||
mongoDatabaseMock
|
||||
.Setup(i => i.GetCollection<FeedbackReceiver>(It.IsAny<string>(), It.IsAny<MongoCollectionSettings>()))
|
||||
.Returns(mongoCollectionMock.Object);
|
||||
|
||||
// Test
|
||||
var service = new FeedbackReceiverService(mongoClientMock.Object, mongoSettingsMock.Object);
|
||||
var ex = Record.ExceptionAsync(async () => await service.CreateManyAsync(null));
|
||||
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(ex);
|
||||
|
||||
mongoClientMock
|
||||
.Verify(i => i.GetDatabase("MyDB", null), Times.Once());
|
||||
mongoDatabaseMock
|
||||
.Verify(i => i.GetCollection<FeedbackReceiver>("feedback_receiver", It.IsAny<MongoCollectionSettings>()),
|
||||
Times.Once());
|
||||
mongoCollectionMock
|
||||
.Verify(i => i.InsertManyAsync(It.IsAny<IEnumerable<FeedbackReceiver>>(), It.IsAny<InsertManyOptions>(), It.IsAny<CancellationToken>()),
|
||||
Times.Never());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Test_CreateManyAsync_NoItems()
|
||||
{
|
||||
// Arrange
|
||||
var mongoDatabaseMock = new Mock<IMongoDatabase>();
|
||||
var mongoClientMock = new Mock<IMongoClient>();
|
||||
var mongoSettingsMock = new Mock<IMongoDbSettings>();
|
||||
var mongoCollectionMock = new Mock<IMongoCollection<FeedbackReceiver>>();
|
||||
|
||||
mongoSettingsMock.SetupGet(i => i.DatabaseName).Returns("MyDB");
|
||||
mongoSettingsMock.SetupGet(i => i.FeedbackReceiverCollectionName).Returns("feedback_receiver");
|
||||
|
||||
mongoClientMock
|
||||
.Setup(i => i.GetDatabase(It.IsAny<string>(), It.IsAny<MongoDatabaseSettings>()))
|
||||
.Returns(mongoDatabaseMock.Object);
|
||||
|
||||
mongoDatabaseMock
|
||||
.Setup(i => i.GetCollection<FeedbackReceiver>(It.IsAny<string>(), It.IsAny<MongoCollectionSettings>()))
|
||||
.Returns(mongoCollectionMock.Object);
|
||||
|
||||
// Test
|
||||
var service = new FeedbackReceiverService(mongoClientMock.Object, mongoSettingsMock.Object);
|
||||
var ex = Record.ExceptionAsync(async () => await service.CreateManyAsync(new List<FeedbackReceiver>()));
|
||||
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(ex);
|
||||
|
||||
mongoClientMock
|
||||
.Verify(i => i.GetDatabase("MyDB", null), Times.Once());
|
||||
mongoDatabaseMock
|
||||
.Verify(i => i.GetCollection<FeedbackReceiver>("feedback_receiver", It.IsAny<MongoCollectionSettings>()),
|
||||
Times.Once());
|
||||
mongoCollectionMock
|
||||
.Verify(i => i.InsertManyAsync(It.IsAny<IEnumerable<FeedbackReceiver>>(), It.IsAny<InsertManyOptions>(), It.IsAny<CancellationToken>()),
|
||||
Times.Never());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using MongoDB.Driver;
|
||||
using Retroactiune.Models;
|
||||
|
@ -23,6 +24,16 @@ namespace Retroactiune.Services
|
|||
|
||||
public async Task CreateManyAsync(IEnumerable<FeedbackReceiver> items)
|
||||
{
|
||||
if (items == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(items));
|
||||
}
|
||||
|
||||
if (!items.Any())
|
||||
{
|
||||
throw new GenericServiceException("items must contain at least one element");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await _collection.InsertManyAsync(items);
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
using System;
|
||||
|
||||
namespace Retroactiune.Settings
|
||||
namespace Retroactiune.Settings
|
||||
{
|
||||
/// <summary>
|
||||
/// Interface for repressing the application's MongoDb settings Options.
|
||||
|
|
Loading…
Reference in a new issue