public void TestUpdateContact() { // Create and set up the mock repository Mock<IContactRepository> mockRepository = new Mock<IContactRepository>(); mockRepository.Setup(x => x.UpdateContact(It.IsAny<Contact>())); // Updating a contact involves calling up the existing record - we need to mock this behaviour. mockRepository.Setup(x => x.GetContactById(It.Is<int>(y => y == 1))).Returns(new Contact { FirstName = "Nigel", LastName = "Richards", DateOfBirth = DateTime.Today, Email = "*****@*****.**", JoinDate = DateTime.Today, Organisation = "Contoso", Phone = "123456", Title = "Developer", Picture = null }); ContactUtilities contactUtilities = new ContactUtilities(mockRepository.Object); // Run the code we want to test, keep the result. bool updateResult = contactUtilities.UpdateContact(1, "Austin", "Morris", "*****@*****.**", "5438756", "BBC", "Broadcaster", DateTime.Today, DateTime.Today, null, null); Assert.IsTrue(updateResult); }
public void TestDeleteContactThrowsException() { Mock<DbSet<Contact>> mockSet = _createContactDbSet(); // Set up the mock repository Mock<IContactRepository> mockRepository = new Mock<IContactRepository>(); mockRepository.Setup(x => x.DeleteContact(It.IsAny<int>())).Throws(new Exception()); // DeleteContact will raise an exception. mockRepository.Setup(x => x.GetContacts()).Returns(mockSet.Object); // Instantiate the biz logic class we plan to test a component of, pass in the mocked repository ContactUtilities contactUtilities = new ContactUtilities(mockRepository.Object); // Run the code we want to test, keep the results bool contactDeleted = contactUtilities.DeleteContact(1); Assert.IsFalse(contactDeleted); }
public void TestValidateEmailFails() { Mock<IContactRepository> mockRepository = new Mock<IContactRepository>(); ContactUtilities contactUtilities = new ContactUtilities(mockRepository.Object); bool result = contactUtilities.ValidateEmail("mr.matt&gmail.com"); Assert.IsFalse(result); }
public void TestDeleteContactWhoIsNotAManager() { Mock<DbSet<Contact>> mockSet = _createContactDbSet(); // Set up the mock repository Mock<IContactRepository> mockRepository = new Mock<IContactRepository>(); mockRepository.Setup(x => x.DeleteContact(It.Is<int>(y => y == 3))); mockRepository.Setup(x => x.GetContacts()).Returns(mockSet.Object); // Instantiate the biz logic class we plan to test a component of, pass in the mocked repository ContactUtilities contactUtilities = new ContactUtilities(mockRepository.Object); // Run the code we want to test, keep the results bool contactDeleted = contactUtilities.DeleteContact(3); Assert.IsTrue(contactDeleted); }
public void TestGetAllContactsExceptOne() { Mock<DbSet<Contact>> mockSet = _createContactDbSet(); // Set up the mock Mock<IContactRepository> mockRepository = new Mock<IContactRepository>(); mockRepository.Setup(x => x.GetContacts()).Returns(mockSet.Object); // Instantiate the biz logic class we plan to test a component of, pass in the mocked repository ContactUtilities contactUtilities = new ContactUtilities(mockRepository.Object); // Run the code we want to test, keep the result. List<Contact> actualContacts = contactUtilities.GetAllContactsExceptOne(1); Assert.AreEqual(2, actualContacts.Count); }
public void TestGetContacts() { // GetAllContacts() returns all contacts, ordered by lastname. Mock<DbSet<Contact>> mockSet = _createContactDbSet(); // Set up the mock Mock<IContactRepository> mockRepository = new Mock<IContactRepository>(); mockRepository.Setup(x => x.GetContacts()).Returns(mockSet.Object); // Instantiate the biz logic class we plan to test a component of, pass in the mocked repository ContactUtilities contactUtilities = new ContactUtilities(mockRepository.Object); // Run the code we want to test, keep the result. List<Contact> actualContacts = contactUtilities.GetAllContacts(); Assert.AreEqual(3, actualContacts.Count); Assert.AreEqual("Arbour", actualContacts[0].LastName); Assert.AreEqual("Kenny", actualContacts[1].LastName); Assert.AreEqual("Morris", actualContacts[2].LastName); }
public void TestGetContactById() { Contact expected = new Contact { FirstName = "Nigel", LastName = "Richards", DateOfBirth = DateTime.Today, Email = "*****@*****.**", JoinDate = DateTime.Today, Organisation = "Contoso", Phone = "123456", Title = "Developer", Picture = null }; // Set up the mock Mock<IContactRepository> mockRepository = new Mock<IContactRepository>(); // Indicate what the mock should accept, and what it should return. mockRepository.Setup(x => x.GetContactById(It.Is<int>(y => y == 1))).Returns(new Contact { FirstName = "Nigel", LastName = "Richards", DateOfBirth = DateTime.Today, Email = "*****@*****.**", JoinDate = DateTime.Today, Organisation = "Contoso", Phone = "123456", Title = "Developer", Picture = null }); // Instantiate the biz logic class we plan to test a component of, pass in the mocked repository ContactUtilities contactUtilities = new ContactUtilities(mockRepository.Object); // Run the code we want to test, keep the result. Contact actual = contactUtilities.GetContactById(1); // Compare with what we expected to get. Assert.AreEqual(expected, actual); }
public HomeController() { _contactUtilities = new ContactUtilities(new ContactRepository(new FabricamContactsDbContext())); }