public async Task DeleteAsync_UsingNonexistentEntityKey_MustThrowException()
        {
            // Arrange
            var inMemoryDatabase =
                new DbContextOptionsBuilder <TodoDbContext>()
                .UseInMemoryDatabase($"db--{Guid.NewGuid():N}")
                .EnableDetailedErrors()
                .EnableSensitiveDataLogging();

            var mockLogger = new Mock <ILogger <TodoItemService> >();

            var owner = new Mock <IPrincipal>();

            owner.SetupGet(x => x.Identity).Returns(new GenericIdentity("test"));

            var deleteTodoItemInfo = new DeleteTodoItemInfo
            {
                Id    = long.MaxValue,
                Owner = owner.Object
            };

            var todoItemService = new TodoItemService(new TodoDbContext(inMemoryDatabase.Options), mockLogger.Object);

            // Act
            Func <Task> deleteAsyncCall = async() => await todoItemService.DeleteAsync(deleteTodoItemInfo);

            // Assert
            await deleteAsyncCall.Should().ThrowExactlyAsync <EntityNotFoundException>(
                "service cannot delete data using nonexistent entity key");
        }
Exemple #2
0
        public Task DeleteAsync(DeleteTodoItemInfo deleteTodoItemInfo)
        {
            if (deleteTodoItemInfo == null)
            {
                throw new ArgumentNullException(nameof(deleteTodoItemInfo));
            }

            Validator.ValidateObject(deleteTodoItemInfo, new ValidationContext(deleteTodoItemInfo),
                                     validateAllProperties: true);

            return(InternalDeleteAsync(deleteTodoItemInfo));
        }
Exemple #3
0
        private async Task InternalDeleteAsync(DeleteTodoItemInfo deleteTodoItemInfo)
        {
            logger.LogInformation("About to delete item using context {@DeleteTodoItemInfo} ...", deleteTodoItemInfo);

            TodoItem existingTodoItem = await GetExistingTodoItem(deleteTodoItemInfo.Id, deleteTodoItemInfo.Owner);

            todoDbContext.TodoItems.Remove(existingTodoItem);
            await todoDbContext.SaveChangesAsync();

            logger.LogInformation("Item with id {TodoItemId} has been deleted by user [{User}]",
                                  deleteTodoItemInfo.Id, deleteTodoItemInfo.Owner.GetName());
        }
        public async Task DeleteAsync_UsingNullAsDeleteTodoItemInfo_MustThrowException()
        {
            // Arrange
            var mockTodoDbContext = new DbContextMock <TodoDbContext>(DummyOptions);
            var mockLogger        = new Mock <ILogger <TodoItemService> >();
            var todoItemService   = new TodoItemService(mockTodoDbContext.Object, mockLogger.Object);
            DeleteTodoItemInfo deleteTodoItemInfo = null;

            // Act
            // ReSharper disable once ExpressionIsAlwaysNull
            Func <Task> deleteAsyncCall = async() => await todoItemService.DeleteAsync(deleteTodoItemInfo);

            // Assert
            await deleteAsyncCall
            .Should().ThrowExactlyAsync <ArgumentNullException>("service cannot delete data using a null item")
            .WithParameterName(nameof(deleteTodoItemInfo), "the item is null");
        }