public async Task EditTodo_NullModel_BadRequest() { // Arrange var service = new MockTodoService(); var controller = new TodoController(service); // Act var result = await controller.EditTodo(null) as BadRequestResult; // Assert Assert.NotNull(result); Assert.Equal(400, result.StatusCode); }
public async Task EditTodo_ValidUserAndTodo_Ok() { // Arrange var service = new MockTodoService { MEditTodoAsync = (model, userId) => { } }; var controller = new TodoController(service); MockClaims.AddUserIdClaim(controller, MockApplicationUsers.Get(0).Id); // Act var result = await controller.EditTodo(MockEditTodoViewModel.Get(0)) as OkResult; // Assert Assert.NotNull(result); Assert.Equal(200, result.StatusCode); }
public async Task EditTodo_InvalidTodo_TodoNotFoundException() { // Arrange var service = new MockTodoService { MEditTodoAsync = (model, userId) => throw new TodoNotFoundException() }; var controller = new TodoController(service); MockClaims.AddUserIdClaim(controller, MockApplicationUsers.Get(0).Id); // Act var result = await controller.EditTodo(MockEditTodoViewModel.Get(0)) as NotFoundResult; // Assert Assert.NotNull(result); Assert.Equal(404, result.StatusCode); }
public async Task Edit_Item() { var itemDetails = new TodoItem { Name = "AName", IsComplete = false }; var result = await ControllerUnderTest.EditTodo(2, itemDetails) as OkObjectResult; var item = result.Value as TodoItem; Assert.AreEqual(item.Name, itemDetails.Name); Assert.AreEqual(item.IsComplete, itemDetails.IsComplete); var editedItem = TodoContext.TodoItems.FirstOrDefault(i => i.Id == 2); Assert.IsNotNull(editedItem); Assert.AreEqual(item.Name, editedItem.Name); Assert.AreEqual(item.IsComplete, itemDetails.IsComplete); }
public async Task EditTodo_InvalidModel_BadRequest() { // Arrange const string field = "required"; const string errorMessage = "something is required"; var service = new MockTodoService(); var controller = new TodoController(service); controller.ModelState.AddModelError(field, errorMessage); var model = MockEditTodoViewModel.Get(0); // Act var result = await controller.EditTodo(model) as BadRequestObjectResult; var error = result?.Value as SerializableError; // Assert Assert.NotNull(error); Assert.Equal(400, result.StatusCode); Assert.Single(error.Keys); Assert.True(error.ContainsKey(field)); Assert.Equal(new[] { errorMessage }, error.GetValueOrDefault(field)); }