예제 #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);
        }
        public Person Info(string name)
        {
            Contract.Requires <ArgumentException>(!string.IsNullOrEmpty(name));
            var pm  = new PersonManagement();
            var pet = pm.GetByName(name);

            return(pet);
        }
예제 #4
0
        public PersonErrorJson(PersonManagement personManagement)
        {
            _personManagement = personManagement;

            if (_personManagement.PersonNotFound)
            {
                Error = "PERSON_NOT_FOUND";
            }
        }
예제 #5
0
        public async Task <ActionResult> Create([FromBody] PersonModel model)
        {
            PersonManagement personManagement = new PersonManagement();

            if (!await personManagement.Create(model.Map()))
            {
                return(new PersonErrorJson(personManagement));
            }

            return(new PersonJson(personManagement.Person));
        }
예제 #6
0
        private void SetControllerContext(string personName)
        {
            Mock <IRepository> _repository = new Mock <IRepository>();

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

            _Controller = new PersonController(pm);
        }
예제 #7
0
        public async Task <ActionResult> Delete(int id)
        {
            PersonManagement personManagement = new PersonManagement();

            await personManagement.Find(id);

            if (!await personManagement.Delete())
            {
                return(new PersonErrorJson(personManagement));
            }

            return(new HttpStatusCodeResult(HttpStatusCode.NoContent));
        }
예제 #8
0
        public async Task <ActionResult> Update(int id, [FromBody] UpdatePersonModel model)
        {
            PersonManagement personManagement = new PersonManagement();
            await personManagement.Find(id);

            model.Map(personManagement.Person);

            if (!await personManagement.Update())
            {
                return(new PersonErrorJson(personManagement));
            }

            return(new HttpStatusCodeResult(HttpStatusCode.NoContent));
        }
        public void GetByName_ExistingPerson_ReturnsPerson()
        {
            // Arrange
            string personName = "Fido";

            _Repository.Setup(x => x.GetByName(It.Is <string>(y => y == "Fido")))
            .Returns(new Person {
                PersonName = "Fido"
            });
            var pm = new PersonManagement(_Repository.Object);
            // Act
            var result = pm.GetByName(personName);

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(personName, result.PersonName);
        }
예제 #10
0
 public PersonController(PersonManagement personManagement)
 {
     _PersonManagement = personManagement;
 }