public async Task UpdateProductAsync_GivenNullProductDTO_ShouldThrowAnException() { IUnitOfWork <IBoltDbContext> unitOfWorkMock = new Mock <IUnitOfWork <IBoltDbContext> >().Object; var service = new ProductsService(unitOfWorkMock); service .Awaiting(async sut => await sut.UpdateProductAsync(2, null)) .Should() .ThrowExactly <ArgumentNullException>() .WithMessage("Value cannot be null.\r\nParameter name: The model cannot be null or empty."); }
public async Task GetProductDetailsAsync_GivenANullProductId_ShouldThrowAnException(int productId) { var unitOfWorkMock = new Mock <IUnitOfWork <IBoltDbContext> >(); var service = new ProductsService(unitOfWorkMock.Object); service .Awaiting(async sut => await sut.GetProductDetailsAsync(productId)) .Should() .ThrowExactly <ArgumentException>() .WithMessage("Failed to get the product details, please try again.") .WithInnerException <Exception>(); }
public async Task GetProductsByIDsAsync_WhenGetRepositoryThrowsAnException_ShouldThrowAnException() { var exceptionToThrow = new ArgumentException(); var unitOfWorkMock = new Mock <IUnitOfWork <IBoltDbContext> >(); unitOfWorkMock.Setup(x => x.GetRepository <IProductsRepository>()).Callback(() => throw exceptionToThrow); IEnumerable <int> productIds = new List <int>(); var service = new ProductsService(unitOfWorkMock.Object); service .Awaiting(async sut => await sut.GetProductsByIDsAsync(productIds)) .Should() .ThrowExactly <ArgumentException>() .WithInnerException <Exception>(); }
public async Task GetAllProductsAsync_WhenAnUnexpectedExceptionIsThrown_ShouldThrowAnException() { var exceptionToThrow = new ArgumentException(); var unitOfWorkMock = new Mock <IUnitOfWork <IBoltDbContext> >(); unitOfWorkMock.Setup(x => x.GetRepository <IProductsRepository>()).Callback(() => throw exceptionToThrow); var service = new ProductsService(unitOfWorkMock.Object); service .Awaiting(async sut => await sut.GetAllProductsAsync()) .Should() .ThrowExactly <ArgumentException>() .WithMessage("Failed to get all products. Please try again.") .WithInnerException <Exception>(); }
public async Task DeleteProductAsync_WhenRemoveThrowsAnException_ShouldThrowAnException() { var exceptionToThrow = new ArgumentException(); var unitofWorkMock = new Mock <IUnitOfWork <IBoltDbContext> >(); unitofWorkMock.Setup(x => x.DbContext.Products.Remove(It.IsAny <Product>())) .Callback(() => throw exceptionToThrow); var service = new ProductsService(unitofWorkMock.Object); service .Awaiting(async sut => await sut.DeleteProductAsync(2)) .Should() .ThrowExactly <ArgumentException>() .WithMessage("Failed to delete the product. Please try again.") .WithInnerException <Exception>(); }
public async Task UpdateProdcutAsync_GivenNullParameters_ShouldThrowAnException(string name, string description) { IUnitOfWork <IBoltDbContext> unitOfWorkMock = new Mock <IUnitOfWork <IBoltDbContext> >().Object; var productDto = new ProductDTO { Name = name, Description = description }; var service = new ProductsService(unitOfWorkMock); service .Awaiting(async sut => await sut.UpdateProductAsync(2, productDto)) .Should() .ThrowExactly <ArgumentException>() .WithMessage("Failed to update the product. Please try again.") .WithInnerException <Exception>(); }