public async Task EditAsync_ThrowsArgumentNullException <TKey>( TKey defaultKey) { // Arrange var mockRepository = new Mock <IRepository <FakeEntity <TKey>, TKey> >(MockBehavior.Strict); var businessService = new FakeBusinessService <TKey>( _mockUnitOfWork.Object, _ => mockRepository.Object, _mockBusinessMapper.Object, _mockLogger.Object ); // Act & Assert Assert.NotNull(defaultKey); var exception = await Assert.ThrowsAsync <ArgumentNullException>( () => businessService.EditAsync(null) ); Assert.Equal("item", exception.ParamName); }
public async Task EditAsync_WhenNotFound_ReturnsNull <TKey>( TKey defaultKey) { // Arrange var item = new FakeEditDto <TKey> { Id = defaultKey }; Expression <Action <ILogger> > loggerExpressionEdit = x => x.Log( LogLevel.Information, 0, It.Is <It.IsAnyType>((v, t) => v.ToString() == $"Edit item: {item}" ), null, It.Is <Func <It.IsAnyType, Exception, string> >((v, t) => true) ); _mockLogger .Setup(x => x.IsEnabled(LogLevel.Information)) .Returns(true); _mockLogger .Setup(x => x.IsEnabled(LogLevel.Warning)) .Returns(true); _mockLogger.Setup(loggerExpressionEdit); var filterExpression = FilterExpressionExtensions.CreateIdFilterExpression <FakeEntity <TKey>, TKey>(defaultKey); var mockRepository = new Mock <IRepository <FakeEntity <TKey>, TKey> >(MockBehavior.Strict); mockRepository .Setup( x => x.GetAsync( It.Is <QueryParameters <FakeEntity <TKey>, TKey> >( y => Lambda.Eq( y.Filter.Expression, filterExpression ) && y.Sort == null && y.Page == null ) ) ) .ReturnsAsync(() => null); Expression <Action <ILogger> > loggerExpressionNotFound = x => x.Log( LogLevel.Warning, 0, It.Is <It.IsAnyType>((v, t) => v.ToString() == $"Item not found: {item.Id}" ), null, It.Is <Func <It.IsAnyType, Exception, string> >((v, t) => true) ); _mockLogger.Setup(loggerExpressionNotFound); var businessService = new FakeBusinessService <TKey>( _mockUnitOfWork.Object, _ => mockRepository.Object, _mockBusinessMapper.Object, _mockLogger.Object ); // Act var result = await businessService.EditAsync(item); // Assert _mockLogger.Verify(loggerExpressionEdit, Times.Once); mockRepository .Verify( x => x.GetAsync( It.Is <QueryParameters <FakeEntity <TKey>, TKey> >( y => Lambda.Eq( y.Filter.Expression, filterExpression ) && y.Sort == null && y.Page == null ) ), Times.Once ); _mockLogger.Verify(loggerExpressionNotFound, Times.Once); Assert.Null(result); }
public async Task EditAsync_Success <TKey>( TKey defaultKey, FakeEditDto <TKey> item, FakeEntity <TKey> entity, FakeDto <TKey> expectedResult) { // Arrange Expression <Action <ILogger> > loggerExpressionEdit = x => x.Log( LogLevel.Information, 0, It.Is <It.IsAnyType>((v, t) => v.ToString() == $"Edit item: {item}" ), null, It.Is <Func <It.IsAnyType, Exception, string> >((v, t) => true) ); _mockLogger .Setup(x => x.IsEnabled(LogLevel.Information)) .Returns(true); _mockLogger.Setup(loggerExpressionEdit); var filterExpression = FilterExpressionExtensions.CreateIdFilterExpression <FakeEntity <TKey>, TKey>(defaultKey); var mockRepository = new Mock <IRepository <FakeEntity <TKey>, TKey> >(MockBehavior.Strict); mockRepository .Setup( x => x.GetAsync( It.Is <QueryParameters <FakeEntity <TKey>, TKey> >( y => Lambda.Eq( y.Filter.Expression, filterExpression ) && y.Sort == null && y.Page == null ) ) ) .ReturnsAsync(entity); _mockBusinessMapper .Setup( x => x.Map(item, entity) ) .Returns(entity); mockRepository .Setup( x => x.Update(entity, false) ) .Returns(entity); Expression <Action <ILogger> > loggerExpressionSave = x => x.Log( LogLevel.Information, 0, It.Is <It.IsAnyType>((v, t) => v.ToString() == "Save changes" ), null, It.Is <Func <It.IsAnyType, Exception, string> >((v, t) => true) ); _mockLogger.Setup(loggerExpressionSave); _mockUnitOfWork .Setup( x => x.CommitAsync() ) .ReturnsAsync(1); _mockBusinessMapper .Setup( x => x.Map <FakeEntity <TKey>, FakeDto <TKey> >(entity) ) .Returns(expectedResult); var businessService = new FakeBusinessService <TKey>( _mockUnitOfWork.Object, _ => mockRepository.Object, _mockBusinessMapper.Object, _mockLogger.Object ); // Act var result = await businessService.EditAsync(item); // Assert _mockLogger.Verify(loggerExpressionEdit, Times.Once); mockRepository .Verify( x => x.GetAsync( It.Is <QueryParameters <FakeEntity <TKey>, TKey> >( y => Lambda.Eq( y.Filter.Expression, filterExpression ) && y.Sort == null && y.Page == null ) ), Times.Once ); _mockBusinessMapper .Verify( x => x.Map(item, entity), Times.Once ); mockRepository .Verify( x => x.Update(entity, false), Times.Once ); _mockLogger.Verify(loggerExpressionSave, Times.Once); _mockUnitOfWork .Verify( x => x.CommitAsync(), Times.Once ); _mockBusinessMapper .Verify( x => x.Map <FakeEntity <TKey>, FakeDto <TKey> >(entity), Times.Once ); Assert.Equal(expectedResult, result, new FakeDtoEqualityComparer <TKey>()); }