public void ShouldRenderIndexViewCorrectly() { Mock <IGenericService <LearningLine> > learningLineServiceMock = new Mock <IGenericService <LearningLine> >(); LearningLine learningLine = new LearningLine() { Id = 100, Name = "Computernetwerken" }; learningLineServiceMock.Setup(m => m.FindAll(It.IsAny <string[]>(), It.IsAny <int>(), It.IsAny <int>())).Returns(new List <LearningLine>() { learningLine, new LearningLine() { Id = 101 }, new LearningLine() { Id = 102 }, }); LearningLineController controller = new LearningLineController(learningLineServiceMock.Object, null, null, null); List <LearningLine> model = (controller.Index() as ViewResult)?.ViewData.Model as List <LearningLine>; Assert.Equal(3, model.Count); Assert.Equal(100, model[0].Id); Assert.Equal(101, model[1].Id); Assert.Equal("Computernetwerken", model[0].Name); }
public IActionResult Delete(int id) { LearningLine learningLine = learningLineService.FindById(id); IActionResult actionResult = View(); if (!ModelState.IsValid) { actionResult = View("Error"); } else if (learningLineService.Delete(learningLine) == 1) { actionResult = RedirectToAction(nameof(Index)); } return(actionResult); }
public void ShouldDeleteCorrectly() { Mock <IGenericService <LearningLine> > learningLineServiceMock = new Mock <IGenericService <LearningLine> >(); Mock <IObjectFinderService <Goal> > goalFinderMock = new Mock <IObjectFinderService <Goal> >(); Mock <IGenericService <Goal> > goalServiceMock = new Mock <IGenericService <Goal> >(); Mock <IManyToManyMapperService <LearningLine, LearningLineGoal, Goal> > mapperServiceMock = new Mock <IManyToManyMapperService <LearningLine, LearningLineGoal, Goal> >(); LearningLine learningLine = new LearningLine() { Id = 100, Name = "Computernetwerken" }; ClaimsPrincipal identity = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.NameIdentifier, "123") })); goalFinderMock.Setup(m => m.GetObjects(It.IsAny <int[]>())).Returns((int[] ids) => { return(new List <Goal>()); }); learningLineServiceMock.Setup(m => m.FindById(It.IsAny <int>(), It.IsAny <string[]>())).Returns(learningLine); learningLineServiceMock.Setup(m => m.Delete(It.IsAny <LearningLine>())).Returns((LearningLine model) => { return(1); }); LearningLineController controller = new LearningLineController(learningLineServiceMock.Object, goalServiceMock.Object, goalFinderMock.Object, mapperServiceMock.Object) { ControllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { User = identity } } }; RedirectToActionResult result = controller.Delete(learningLine.Id) as RedirectToActionResult; Assert.Equal("Index", result?.ActionName); }
public void ShouldRenderDetailViewCorrectly() { Mock <IGenericService <LearningLine> > learningLineServiceMock = new Mock <IGenericService <LearningLine> >(); LearningLine learningLine = new LearningLine() { Id = 100, Name = "Computernetwerken" }; learningLineServiceMock.Setup(m => m.FindById(It.IsAny <int>(), It.IsAny <string[]>())).Returns(learningLine); LearningLineController controller = new LearningLineController(learningLineServiceMock.Object, null, null, null); LearningLine model = (controller.Details(learningLine.Id) as ViewResult)?.ViewData.Model as LearningLine; Assert.Equal(100, model.Id); Assert.Equal("Computernetwerken", model.Name); }
// GET: Goal/Edit/5 /// <summary> /// Represents an <see cref="IActionResult"/> that renders the the <see cref="Edit(int)"/> view to the response. /// </summary> /// <param name="id">The ID of the goal to edit.</param> /// <returns>A view of the <see cref="Edit(int)"/> action method.</returns> public IActionResult Edit(int id) { IActionResult actionResult = View(); ViewData["Goals"] = goalService.FindAll(); LearningLineCreateUpdateViewModel llcuvm = new LearningLineCreateUpdateViewModel(); LearningLine ll = learningLineService.FindById(id, new string[] { "Goals.Goal" }); llcuvm.GoalsIds = ll.Goals.Select(e => e.GoalId) as List <int>; llcuvm.Goals = ll.Goals; llcuvm.Name = ll.Name; if (!ModelState.IsValid) { actionResult = View("Error"); } else { actionResult = View(llcuvm); } return(actionResult); }