public void AddAgentMessage() { using (var uow = new CapriconContext()) { //retreive an existing agent var agentRepository = new AgentRepository(uow); var existingAgent = agentRepository.GetAll().FirstOrDefault(); Assert.IsNotNull(existingAgent); //retreive an existing message var messageRepository = new MessageRepository(uow); var existingMessage = messageRepository.GetAll().LastOrDefault(); Assert.IsNotNull(existingMessage); //create new agent messsage var newAgentMessage = new AgentMessage() { Agent = existingAgent, Message = existingMessage }; //add the new agent message to the repository var agentMessageRepository = new AgentMessageRepository(uow); agentMessageRepository.Add(newAgentMessage); try { uow.SaveChanges(); } catch (DbEntityValidationException ex) { //Retrieve validation errors ex.EntityValidationErrors.ToList().ForEach ( v => { v.ValidationErrors.ToList().ForEach ( e => { System.Diagnostics.Debug.WriteLine(e.ErrorMessage); } ); } ); Assert.Fail("Test failed"); } //retrieve saved object var uow1 = new CapriconContext(); var repository = new AgentMessageRepository(uow1); var savedAgentMessages = repository.GetAll().ToList(); Assert.AreEqual(savedAgentMessages[0].Agent.FirstName, existingAgent.FirstName = "Blaise"); Assert.AreEqual(savedAgentMessages[0].Message.MessageId, existingMessage.MessageId = 1); }; }
public void UpdateAgentMessage() { using (var uow = new CapriconContext()) { var agentMessageRepository = new AgentMessageRepository(uow); var existingAgentMessage = agentMessageRepository.Find(am => am.Id == 1).FirstOrDefault(); Assert.IsNotNull(existingAgentMessage); //retreive an existing agent var agentRepository = new AgentRepository(uow); var existingAgent = agentRepository.Find(a => a.AgentId == 1).FirstOrDefault(); Assert.IsNotNull(existingAgent); //retreive an existing message var messageRepository = new MessageRepository(uow); var existingMessage = messageRepository.Find(m => m.MessageId == 1).FirstOrDefault(); Assert.IsNotNull(existingMessage); //edit an existing agent message existingAgentMessage.Agent = existingAgent; existingAgentMessage.Message = existingMessage; agentMessageRepository.Attach(existingAgentMessage); uow.Entry(existingAgentMessage).State = EntityState.Modified; try { uow.SaveChanges(); } catch (DbEntityValidationException ex) { //Retrieve validation errors ex.EntityValidationErrors.ToList().ForEach ( v => { v.ValidationErrors.ToList().ForEach ( e => { System.Diagnostics.Debug.WriteLine(e.ErrorMessage); } ); } ); Assert.Fail("Test failed"); } //retrieve saved object var uow1 = new CapriconContext(); var repository = new AgentMessageRepository(uow1); var savedAgentMessages = repository.GetAll().ToList(); Assert.AreEqual(savedAgentMessages[0].Agent, existingAgent); Assert.AreEqual(savedAgentMessages[0].Message, existingMessage); }; }