public void Delete_Person_Returns_Response_Message_With_No_Content_Status_Code()
        {
            // Arrange
            var service = Mock.Of<IPersonService>();
            var controller = new PersonController(service);

            // Act
            var result = controller.Delete(ObjectId.GenerateNewId().ToString()) as OkResult;

            // Assert
            Assert.NotNull(result);
        }
        public void Delete_Person_Runs_Service_Method()
        {
            // Arrange
            var removedId = ObjectId.GenerateNewId().ToString();
            var service = Mock.Of<IPersonService>();
            var controller = new PersonController(service);

            // Act
            controller.Delete(removedId);

            // Assert
            Mock.Get(service).Verify(s => s.Delete(removedId), Times.Once);
        }
        public void Get_All_Persons()
        {
            // Arrange
            var allPersons = new[]
            {
                new Library.Entity.Person {FirstName = "John", LastName = "McClung", MiddleName = "Paul", Notes = "Crazy as can be"},
                new Library.Entity.Person {FirstName = "Katie", LastName = "McClung", MiddleName = "Elane", Notes = "Just as crazy as John"}
            };
            var service = Mock.Of<IPersonService>(s => s.All(null, null, "LastName") == allPersons.AsEnumerable());
            var controller = new PersonController(service);

            // Act
            var result = controller.Get(null, null, "LastName") as OkNegotiatedContentResult<IEnumerable<Web.Models.Person>>;
            var personModels = allPersons.AsEnumerable().ToModel();

            // Assert
            Assert.NotNull(result);
            Assert.True(personModels.SequenceEqual(result.Content));
        }
        public void Get_Person_By_Id_Test()
        {
            // Arrange
            var person = new Library.Entity.Person
            {
                FirstName = "John",
                LastName = "McClung",
                MiddleName = "Paul",
                Notes = "Crazy as can be",
                Id = ObjectId.GenerateNewId()
            };
            var service = Mock.Of<IPersonService>(s => s.Get(person.Id.ToString()) == person);
            var controller = new PersonController(service);
            var personModel = Mapper.Map<Library.Entity.Person, Web.Models.Person>(person);

            // Act
            var result = controller.Get(person.Id.ToString()) as OkNegotiatedContentResult<Web.Models.Person>;

            // Assert
            Assert.NotNull(result);
            Assert.Equal(personModel, result.Content);
        }
        public void Put_Person_Throws_When_Update_fails()
        {
            // Arrange
            var person = new Web.Models.Person {Id = ObjectId.GenerateNewId().ToString()};
            var service = new Mock<IPersonService>();
            service.Setup(s => s.Update(It.IsAny<Library.Entity.Person>())).Throws<Exception>();
            var controller = new PersonController(service.Object);

            // Act
            var result = controller.Put(person.Id, person) as ExceptionResult;

            // Assert
            Assert.NotNull(result);
        }
        public void Put_Person_Sets_Id_Before_Update()
        {
            // Arrange
            var updatedId = ObjectId.GenerateNewId();
            var service = Mock.Of<IPersonService>();
            var controller = new PersonController(service);

            // Act
            controller.Put(updatedId.ToString(), new Web.Models.Person());

            // Assert
            Mock.Get(service).Verify(s => s.Update(It.Is<Library.Entity.Person>(p => p.Id == updatedId)), Times.Once);
        }
        public void Put_Person_Runs_Service_Update()
        {
            // Arrange
            var person = new Web.Models.Person
            {
                FirstName = "John",
                LastName = "McClung",
                MiddleName = "Paul",
                Notes = "Crazy as can be",
                Id = ObjectId.GenerateNewId().ToString()
            };
            var service = Mock.Of<IPersonService>();
            var controller = new PersonController(service);

            // Act
            controller.Put(person.Id, person);

            // Assert
            Mock.Get(service).Verify(s =>
                s.Update(It.Is<Library.Entity.Person>
                    (e => e.Id.ToString() == person.Id)), Times.Once);
        }
        public void Post_Person_return_created_status_code()
        {
            // Arrange
            var service = Mock.Of<IPersonService>();
            var controller = new PersonController(service);

            // Act
            var result = controller.Post(new Web.Models.Person()) as CreatedAtRouteNegotiatedContentResult<Web.Models.Person>;

            // Assert
            Assert.NotNull(result);
            Assert.Equal("DefaultApi", result.RouteName);
            Assert.NotNull(result.RouteValues["Id"]);
        }
        public void Get_Person_Throws_when_service_returns_null()
        {
            // Arrange
            var id = new ObjectId().ToString();
            var service = new Mock<IPersonService>();
            service.Setup(s => s.Get(id)).Returns((Library.Entity.Person) null);// Had trouble making the static setup work like in the other tests
            var controller = new PersonController(service.Object);

            // Act
            var result = controller.Get(id);

            // Assert
            Assert.IsType<NotFoundResult>(result);
        }