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 void InsertContact(Contact contact) { context.Contacts.Add(contact); }
public void UpdateContact(Contact contact) { context.Entry(contact).State = EntityState.Modified; }
/// <summary> /// Create new contact. /// </summary> /// <remarks> /// Set up relationship between worker and manager of necessary. /// </remarks> /// <param name="firstName"></param> /// <param name="lastName"></param> /// <param name="email"></param> /// <param name="phone"></param> /// <param name="organisation"></param> /// <param name="title"></param> /// <param name="dateOfBirth"></param> /// <param name="dateJoined"></param> /// <param name="picture">Picture to store in database.</param> /// <param name="managerId">The direct manager of this contact.</param> public bool CreateContact(string firstName, string lastName, string email, string phone, string organisation, string title, DateTime dateOfBirth, DateTime dateJoined, Image picture = null, int? managerId = null) { bool contactCreated = true; try { Contact newContact = new Contact { FirstName = firstName, LastName = lastName, Email = email, Phone = phone, Organisation = organisation, Title = title, DateOfBirth = dateOfBirth, JoinDate = dateJoined, ManagerId = managerId }; if (picture != null) { // Use provided image byte[] imageBytes = ImageBytesConverter.ConvertImageToBytes(picture); newContact.Picture = imageBytes; } // Save _contactRepository.InsertContact(newContact); _contactRepository.Save(); } catch (Exception exception) { contactCreated = false; } return contactCreated; }