public Task <long> AddAsync(NewTodoItemInfo newTodoItemInfo) { Validator.ValidateObject(newTodoItemInfo, new ValidationContext(newTodoItemInfo), validateAllProperties: true); return(InternalAddAsync(newTodoItemInfo)); }
public async Task AddAsync_UsingNullAsNewTodoItemInfo_MustThrowException() { // Arrange var mockTodoDbContext = new DbContextMock <TodoDbContext>(DummyOptions); var mockLogger = new Mock <ILogger <TodoItemService> >(); var todoItemService = new TodoItemService(mockTodoDbContext.Object, mockLogger.Object); NewTodoItemInfo newTodoItemInfo = null; // Act // ReSharper disable once ExpressionIsAlwaysNull Func <Task <long> > addAsyncCall = async() => await todoItemService.AddAsync(newTodoItemInfo); // Assert await addAsyncCall .Should() .ThrowExactlyAsync <ArgumentNullException>("service cannot add data using a null item"); }
private async Task <long> InternalAddAsync(NewTodoItemInfo newTodoItemInfo) { logger.LogInformation("About to add item using context {@NewTodoItemInfo} ...", newTodoItemInfo); var newTodoItem = new TodoItem(newTodoItemInfo.Name, newTodoItemInfo.Owner.GetName()); if (newTodoItemInfo.IsComplete.HasValue) { newTodoItem.IsComplete = newTodoItemInfo.IsComplete.Value; } await todoDbContext.TodoItems.AddAsync(newTodoItem); await todoDbContext.SaveChangesAsync(); logger.LogInformation("Item with id {TodoItemId} has been added by user [{User}]" , newTodoItem.Id, newTodoItem.CreatedBy); return(newTodoItem.Id); }