public void DomainTodoSetupDueToday_Constructor_ReturnsVMTodoWithDueTodayTrue() { //Arrange SetupDomainObject(); //Act _todoViewModel = TodoModelFactory.MapDomainModelToViewModel(_domainTodo); //Assert Assert.True(_todoViewModel.IsDueToday); }
/// <summary> /// Convert the TodoViewModel object to DomainModel. This will be invoked when we are creating/updating todos. /// </summary> /// <param name="viewModel"></param> /// <returns></returns> public static Todo MapViewModelToDomainModel(TodoViewModel viewModel) { return new Todo() { Description = viewModel.Description, CategoryId = viewModel.CategoryId, DueOn = viewModel.DueOn, Id = viewModel.Id, IsComplete = viewModel.IsComplete, Location = viewModel.Location, UserId = viewModel.UserId, }; }
public void ModelStateIsValid_PostTodo_InvokesTodoServiceAddAndReturnsStatusCodeOk() { //Arrange //Setup the todoviewmodel with predefined values for id, description and location properties. var categoryId = Guid.NewGuid(); var todo = new TodoViewModel { CategoryId = categoryId, Description = "description1", Location = "Ralphs"}; //The return type of the PostTodo function is HttpResponseMessage and we require the HttpRequest object to // create a response. We are setting up the HttpRequest object for the controller in the following lines of code. _controller.Request = new HttpRequestMessage(); _controller.Request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration()); //Act var result = _controller.PostTodo(todo); //Assert //Now verify that the when PostTodo is invoked with a todoviewmodel object, the todoservice Add is invoked exactly // once with the tododomain object that has the same values for all corresponding properties as setup in the // todoviewmodel. _mockTodoService.Verify(x => x.Add(It.Is<Data.Models.Todo>( y => y.Description.Equals("description1") && y.CategoryId == categoryId && y.Location == "Ralphs")), Times.Once()); //Also verify that when a todoservice call add the new todoitem, controller returns a http status code ok OK. Assert.Equal(HttpStatusCode.OK, result.StatusCode); //Verify that the returned object after a successful Post call is a not null object. Assert.NotNull(result.Content); }
public HttpResponseMessage PostTodo(TodoViewModel todo) { if (!ModelState.IsValid) { return Request.CreateResponse(HttpStatusCode.BadRequest); } var domainTodo = new Todo() { Description = todo.Description, CategoryId = todo.CategoryId, Location = todo.Location, DueOn = todo.DueOn, UserId = User.Identity.GetUserId() }; _todoService.Add(domainTodo); return Request.CreateResponse(HttpStatusCode.OK, todo); }
public void ModelStateIsInvalid_PostTodo_ReturnsBadRequest() { //Arrange var todoGuid = Guid.NewGuid(); var todo = new TodoViewModel { Id = todoGuid, Description = "description1" }; //The return type of the PostTodo function is HttpResponseMessage and we require the HttpRequest object to // create a response. We are setting up the HttpRequest object for the controller in the following lines of code. _controller.Request = new HttpRequestMessage(); _controller.Request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration()); // Setup the model state to have an error so that the Model.IsValid call will return false. _controller.ModelState.AddModelError("Category Id", "Category Id Empty"); //Act var result = _controller.PostTodo(todo); //Assert //Verify that the result has a status code. Assert.Equal(HttpStatusCode.BadRequest, result.StatusCode); }
public void TodoServiceUpdatesTodo_PutTodo_Returns_NoContent() { //Arrange //Setup todoviewmodel object with valid properties such that the PutTodo api would successfully update the todoitem. var todoGuid = Guid.NewGuid(); var dueOn = DateTime.Now; var todo = new TodoViewModel { Id = todoGuid, Description = "description1", DueOn = dueOn, Location = "Costco" }; //Act var result = _controller.PutTodo(todoGuid, todo) as StatusCodeResult; //Assert // Once the todoitem is updated successfully, verify that the return code that the api returns is no content. Assert.Equal(HttpStatusCode.NoContent, result.StatusCode); }
public void TodoIdDoesNotExistTodoServiceReturnsTodoNotFoundException_PutTodo_ReturnsNotFound() { //Arrange var todoId = Guid.NewGuid(); var todoViewModel = new TodoViewModel() {Id = todoId}; //We are setting up the mock todoservice to return a TodoNotFoundException when // todoService is invoked with a todomodel that has a id that equals todoId. _mockTodoService.Setup(x => x.Update(It.Is<Todo>(y => y.Id == todoId))).Throws(new TodoNotFoundException()); //Act var response = _controller.PutTodo(todoId, todoViewModel); //Assert // Verify that the response returned is 404. Assert.IsType<NotFoundResult>(response); }
public void ValidModal_PutTodo_CallsServiceUpdate() { //Arrange //Setup the todoviewmodel with predefined values for id, description and location properties. var todoGuid = Guid.NewGuid(); var dueOn = DateTime.Now; var todo = new TodoViewModel { Id = todoGuid, Description = "description1", DueOn = dueOn, Location = "Costco" } ; //Act var result = _controller.PutTodo(todoGuid, todo); //Assert //Now verify that the when PutTodo is invoked with a todoviewmodel object, the todoservice is invoked exactly // once with the tododomain object that has the same values for all corresponding properties as setup in the // todoviewmodel. _mockTodoService.Verify(m => m.Update(It.Is<Todo>(x => x.Id == todo.Id && x.Description == todo.Description && x.Location == todo.Location)), Times.Once()); }
public void InvalidModel_PutTodo_ReturnsBadRequest() { //Arrange // Setup the model state to have an error so that the Model.IsValid call will return false. var todoGuid = Guid.NewGuid(); var todo = new TodoViewModel { Id = todoGuid, Description = "description1"} ; _controller.ModelState.AddModelError("Category Id", "Category Id Empty"); //Act var result = _controller.PutTodo(todoGuid, todo); //Assert // Verify that the response from the api PutTodo call returns a result of type InvalidModelStateResult // which is a action result that translates to BadRequest response Assert.IsType<InvalidModelStateResult>(result); }