public void FindContactById() { using (IDataContextAsync fakeDbContext = new UnitTestFakeDbContext()) using (IUnitOfWork unitOfWork = new UnitOfWorkBase(fakeDbContext)) { unitOfWork.Repository<Contact>().Insert(new Contact { ContactId = 1, ObjectState = ObjectState.Added }); unitOfWork.Repository<Contact>().Insert(new Contact { ContactId = 2, ObjectState = ObjectState.Added }); unitOfWork.Repository<Contact>().Insert(new Contact { ContactId = 3, ObjectState = ObjectState.Added }); unitOfWork.SaveChanges(); var contact = unitOfWork.Repository<Contact>().Find(2); Assert.IsNotNull(contact); Assert.AreEqual(2, contact.ContactId); } }
public void DeleteContactById() { using (IDataContextAsync fakeDbContext = new UnitTestFakeDbContext()) using (IUnitOfWork unitOfWork = new UnitOfWorkBase(fakeDbContext)) { unitOfWork.Repository<Contact>().Insert(new Contact { ContactId = 1, ObjectState = ObjectState.Added }); unitOfWork.SaveChanges(); unitOfWork.Repository<Contact>().Delete(1); unitOfWork.SaveChanges(); var contact = unitOfWork.Repository<Contact>().Find(1); Assert.IsNull(contact); } }
public void InsertRangeOfContacts() { using (IDataContextAsync fakeDbContext = new UnitTestFakeDbContext()) using (IUnitOfWork unitOfWork = new UnitOfWorkBase(fakeDbContext)) { var newContacts = new[] { new Contact {ContactId = 1, ObjectState = ObjectState.Added}, new Contact {ContactId = 2, ObjectState = ObjectState.Added}, new Contact {ContactId = 3, ObjectState = ObjectState.Added} }; unitOfWork.Repository<Contact>().InsertRange(newContacts); var savedContacts = unitOfWork.Repository<Contact>().Query().Select(); Assert.AreEqual(savedContacts.Count(), newContacts.Length); } }
public void UpdateContact() { using (IDataContextAsync fakeDbContext = new UnitTestFakeDbContext()) using (IUnitOfWork unitOfWork = new UnitOfWorkBase(fakeDbContext)) { unitOfWork.Repository<Contact>().Insert(new Contact { ContactId = 2, FirstName = "Steve", ObjectState = ObjectState.Added }); unitOfWork.SaveChanges(); var contact = unitOfWork.Repository<Contact>().Find(2); Assert.AreEqual(contact.FirstName, "Steve", "Assert we are able to find the inserted Contact."); contact.FirstName = "Mike"; contact.ObjectState = ObjectState.Modified; unitOfWork.Repository<Contact>().Update(contact); unitOfWork.SaveChanges(); Assert.AreEqual(contact.FirstName, "Mike", "Assert that our changes were saved."); } }