Пример #1
0
        public void Display_NonExistingPerson_ReturnNotFoundView()
        {
            // Arrange
            string             personName  = "Barney";
            Mock <IRepository> _repository = new Mock <IRepository>();

            _repository.Setup(x => x.GetByName(It.Is <string>(y => y == "Fido")))
            .Returns(new Person {
                PersonName = "Fido"
            });
            PersonManagement pm         = new PersonManagement(_repository.Object);
            PersonController controller = new PersonController(pm);

            RouteData routeData = new RouteData();

            routeData.Values.Add("id", personName);
            ControllerContext context = new ControllerContext {
                RouteData = routeData
            };

            controller.ControllerContext = context;

            // Act
            var result = controller.Display() as RedirectToRouteResult;

            // Assert
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult));
            Assert.AreEqual("NotFound", result.RouteValues["action"]);
        }
Пример #2
0
        public void Display_ExistingPerson_ReturnView()
        {
            // Arrange
            Mock <IRepository> _repository = new Mock <IRepository>();

            _repository.Setup(x => x.GetByName(It.Is <string>(y => y == "Fido")))
            .Returns(new Person {
                PersonName = "Fido"
            });
            PersonManagement pm         = new PersonManagement(_repository.Object);
            PersonController controller = new PersonController(pm);
            string           personName = "Fido";
            RouteData        routeData  = new RouteData();

            routeData.Values.Add("id", personName);
            ControllerContext context = new ControllerContext {
                RouteData = routeData
            };

            controller.ControllerContext = context;

            // Act
            ViewResult result = (ViewResult)controller.Display();

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual("", result.ViewName);
            Assert.IsNotNull(result.Model);
            Assert.IsInstanceOfType(result.Model, typeof(Person));
            Assert.AreEqual(personName, ((Person)result.Model).PersonName);
        }