public void Add_AddsEmployeeAndReturnsARedirect_WhenModelStateIsValid() { //Arrange var mockCPUService = new Mock <IService <IRepository <CPU>, CPU> >(); mockCPUService.Setup(repo => repo.GetByIdAsync(1)).ReturnsAsync(GetCPU()) .Verifiable(); var httpContext = new DefaultHttpContext(); var tempData = new TempDataDictionary(httpContext, Mock.Of <ITempDataProvider>()); var controller = new CPUController(mockCPUService.Object) { TempData = tempData }; controller.ModelState.AddModelError("Quantity", "Required"); var inputModel = new PCItemInputModel() { Id = 1, Quantity = 1 }; // Act var result = controller.Add(inputModel); // Assert Assert.IsType <JsonResult>(result.Result); mockCPUService.Verify(); }
public void Add_ReturnsBadRequestResult_WhenModelStateIsInvalid() { // Arrange var mockCPUService = new Mock <IService <IRepository <CPU>, CPU> >(); var inputModel = new PCItemInputModel() { Id = 0, Quantity = 0 }; var controller = new CPUController(mockCPUService.Object); controller.ModelState.AddModelError("Quantity", "Required"); // Act var result = controller.Add(inputModel); // Assert Assert.IsType <BadRequestResult>(result.Result); }