public async Task GivenTodoItemExist_WhenPut_ThenTodoItemUpdated() { using (var context = new TodoContext(_contextOptions)) { TodoItemsController controller = new TodoItemsController(context); int id = GetRandomId(context); TodoItem toChange = await context.TodoItems.FirstOrDefaultAsync(t => t.Id == id); String newName = "Don't do this at home"; toChange.Name = newName; var changeResult = await controller.PutTodoItem(id, new TodoItemDTO(toChange)); Assert.AreEqual(typeof(NoContentResult), changeResult.GetType()); TodoItem freshFetched = await context.TodoItems.FirstOrDefaultAsync(t => t.Id == id); Assert.AreEqual(newName, freshFetched.Name); } }
public async Task GivenTodoItemWithDifferentId_WhenPut_ThenReturnBadRequest() { using (var context = new TodoContext(_contextOptions)) { TodoItemsController controller = new TodoItemsController(context); int id = GetRandomId(context); TodoItem toChange = await context.TodoItems.FirstOrDefaultAsync(t => t.Id == id); String oldName = toChange.Name; String newName = "Don't do this at home"; toChange.Name = newName; toChange.Id = toChange.Id == 1 ? 2 : 1; var changeResult = await controller.PutTodoItem(id, new TodoItemDTO(toChange)); Assert.AreEqual(typeof(BadRequestResult), changeResult.GetType()); TodoItem freshFetched = await context.TodoItems.FirstOrDefaultAsync(t => t.Id == id); Assert.AreEqual(oldName, freshFetched.Name); } }