public void Create_Post_PutsValidWithoutError()
        {
            InMemoryContactRepository repository = new InMemoryContactRepository();
            HomeController controller = GetHomeController(repository);

            Contact contact = GetContact(5, "First5", "Last5");
            controller.Create(contact);

            IEnumerable<Contact> contacts = repository.GetAllContacts();
            Assert.IsTrue(contacts.Contains(contact));
        }
        public void Create_Post_ReturnViewWithDatabaseError()
        {
            InMemoryContactRepository repository = new InMemoryContactRepository();
            Exception exception = new Exception();
            repository.ExceptionToThrow = exception;
            HomeController controller = GetHomeController(repository);
            Contact model = GetContact(5,"First5","last5");

            var result = (ViewResult)controller.Create(model);

            Assert.AreEqual("Create", result.ViewName);
            ModelState modelState = result.ViewData.ModelState[""];
            Assert.IsNotNull(modelState);
            Assert.IsTrue(modelState.Errors.Any());
            Assert.AreEqual(exception, modelState.Errors[0].Exception);
            Assert.AreEqual("Unable to create!", result.ViewData["CreateError"]);
        }
 public void Delete_Get_DisplayDeletePage()
 {
     InMemoryContactRepository repository = new InMemoryContactRepository();
     Contact contact = GetContact();
     repository.Add(contact);
     HomeController controller = GetHomeController(repository);
     ViewResult result = (ViewResult)controller.Delete(contact.Id);
     Assert.AreEqual(contact, result.ViewData.Model);
 }
        public void Index_Get_RetrievesAllContactsFromRepository()
        {
            Contact contact1 = GetContact(1, "first1", "last1");
            Contact contact2 = GetContact(2, "first2", "last2");

            InMemoryContactRepository repository = new InMemoryContactRepository();

            repository.Add(contact1);
            repository.Add(contact2);

            var controller = GetHomeController(repository);

            var result = controller.Index();

            var model = (IEnumerable<Contact>)result.ViewData.Model;

            CollectionAssert.Contains(model.ToList(), contact1);
            CollectionAssert.Contains(model.ToList(), contact2);
        }
        public void Edit_Post_EditContactWithoutError()
        {
            InMemoryContactRepository repository = new InMemoryContactRepository();
            Contact contact = GetContact(1, "firstname1", "lastname");
            repository.Add(contact);
            var controller = GetHomeController(repository);

            Contact contactToEdit = repository.GetContactByID(1);

            FormCollection collection = new FormCollection();
            collection["Id"] = contactToEdit.Id.ToString();
            collection["FirstName"] = "firstname";
            collection["LastName"] = contactToEdit.LastName;
            collection["Phone"] = contactToEdit.Phone;
            collection["Email"] = contactToEdit.Email;
            contactToEdit.LastName = "firstname";
            controller.Edit(1,collection);

            Assert.AreEqual(contactToEdit, repository.GetContactByID(1));
        }
        public void Edit_Post_EditContactWithError()
        {
            InMemoryContactRepository repository = new InMemoryContactRepository();
            Exception exception = new Exception();
            repository.ExceptionToThrow = exception;

            Contact contact = GetContact(1, "firstname1", "lastname");
            repository.Add(contact);
            var controller = GetHomeController(repository);

            Contact contactToEdit = repository.GetContactByID(1);

            FormCollection collection = GetFormCollection(contactToEdit, "firstname");

            var result = (ViewResult)controller.Edit(1, collection);

            ModelState modelState = result.ViewData.ModelState[""];
            Assert.IsNotNull(modelState);
            Assert.IsTrue(modelState.Errors.Any());
            Assert.AreEqual(exception, modelState.Errors[0].Exception);
            Assert.AreEqual("Unable to Edit!", result.ViewData["CreateError"]);
        }
        public void Edit_Get_GetEditPageById()
        {
            InMemoryContactRepository repository = new InMemoryContactRepository();
            Contact contact = GetContact(1, "firstname1", "lastname");
            repository.Add(contact);
            var controller = GetHomeController(repository);

            ViewResult result = controller.Edit(1);
            Assert.AreEqual(contact, result.ViewData.Model);
        }
 public void Detail_Get_GetContactDetailsById()
 {
     InMemoryContactRepository repository = new InMemoryContactRepository();
     Contact contact = GetContact(3, "firstName3", "lastName3");
     repository.Add(contact);
     HomeController controller = GetHomeController(repository);
     Assert.AreEqual(contact, controller.Details(contact.Id).ViewData.Model);
 }
 public void Delete_Post_DeleteContactById()
 {
     InMemoryContactRepository repository = new InMemoryContactRepository();
     Contact contact = GetContact();
     repository.Add(contact);
     HomeController controller = GetHomeController(repository);
     Assert.AreEqual(1, repository.GetAllContacts().Count());
     ViewResult result = (ViewResult)controller.Delete(contact.Id, GetFormCollection(contact, ""));
     Assert.AreEqual(0,repository.GetAllContacts().Count());
 }