public void CanDeletePerson() { Mock<IRepository<Person>> mock_person = new Mock<IRepository<Person>>(); Mock<IRepository<Section>> mock_section = new Mock<IRepository<Section>>(); mock_section.Setup(m => m.Repository).Returns(CreateSectionTab().AsQueryable()); mock_person.Setup(m => m.Repository).Returns(CreatePersonTab().AsQueryable()); PersonController controller = new PersonController(mock_person.Object, mock_section.Object); DeleteObjectById model = new DeleteObjectById() { Description = "XXXX", Id = 5 }; var redirectToRouteResult = controller.Delete(model) as RedirectToRouteResult; mock_person.Verify(m => m.DeleteObject(It.IsAny<Person>()), Times.Once()); Assert.IsNotNull(redirectToRouteResult); Assert.AreEqual("Index", redirectToRouteResult.RouteValues["Action"]); }
public void CantDeletePerson() { Mock<HttpRequestBase> request = new Mock<HttpRequestBase>(); Mock<HttpResponseBase> response = new Mock<HttpResponseBase>(); Mock<HttpContextBase> context = new Mock<HttpContextBase>(); context.Setup(c => c.Request).Returns(request.Object); context.Setup(c => c.Response).Returns(response.Object); //Add XMLHttpRequest request header request.Setup(req => req["X-Requested-With"]). Returns("XMLHttpRequest"); Mock<IRepository<Person>> mock_person = new Mock<IRepository<Person>>(); Mock<IRepository<Section>> mock_section = new Mock<IRepository<Section>>(); mock_section.Setup(m => m.Repository).Returns(CreateSectionTab().AsQueryable()); mock_person.Setup(m => m.Repository).Returns(CreatePersonTab().AsQueryable()); PersonController controller = new PersonController(mock_person.Object, mock_section.Object); controller.ControllerContext = new ControllerContext( context.Object, new RouteData(), controller); DeleteObjectById model = new DeleteObjectById() { Description = "XXXX", Id = 155 }; var viewResult = controller.Delete(model.Id) as PartialViewResult; Assert.IsNotNull(viewResult); Assert.AreEqual("_Info", viewResult.ViewName); Assert.AreEqual("Podany pracownik nie istnieje", ((InfoModel)viewResult.Model).Description); Assert.AreEqual("Index", ((InfoModel)viewResult.Model).Action); Assert.AreEqual("Person", ((InfoModel)viewResult.Model).Controller); mock_person.Verify(m => m.DeleteObject(It.IsAny<Person>()), Times.Never()); }