public async Task GetAllAgents_ExistingAgents_NoError()
        {
            // Arrange
            DbContextOptions <YellowJacketContext> options = new DbContextOptionsBuilder <YellowJacketContext>()
                                                             .UseInMemoryDatabase("GetAllAgent_ExistingAgents_NoError")
                                                             .Options;

            List <AgentModel> expectedAgents = new List <AgentModel>
            {
                new AgentModel
                {
                    Name         = "AgentA",
                    LastUpdateOn = DateTime.Now,
                    RegisteredOn = DateTime.Now,
                    Status       = AgentStatus.Idle.ToString()
                },
                new AgentModel
                {
                    Name         = "AgentB",
                    LastUpdateOn = DateTime.Now,
                    RegisteredOn = DateTime.Now,
                    Status       = AgentStatus.Idle.ToString()
                }
            };

            using (YellowJacketContext context = new YellowJacketContext(options))
            {
                IAgentRepository agentRepository = new AgentRepository(context);

                IAgentService service = new AgentService(agentRepository, GetMapper());

                foreach (AgentModel model in expectedAgents)
                {
                    await service.Add(model);
                }

                context.SaveChanges();
            }

            List <AgentModel> actualAgents;

            // Act
            using (YellowJacketContext context = new YellowJacketContext(options))
            {
                IAgentRepository agentRepository = new AgentRepository(context);

                IAgentService service = new AgentService(agentRepository, GetMapper());

                actualAgents = await service.GetAll();
            }

            Assert.That(
                actualAgents.Count,
                Is.EqualTo(2),
                $"The actual agent list count {actualAgents.Count} should be equal to the expected one {expectedAgents.Count}");
        }
Exemple #2
0
 public ActionResult Create(AgentModel am)
 {
     if (ModelState.IsValid)
     {
         Agent a = new Agent
         {
             FirstName     = am.FirstName,
             LastName      = am.LastName,
             PhoneNumber   = am.PhoneNumber,
             Type          = am.Type,
             Heure_travail = am.Heure_travail
         };
         es.Add(a);
         es.Commit();
         return(RedirectToAction("Index"));
     }
     return(View(am));
 }
        public async Task AddAgent_ValidAgent_NoError()
        {
            // Arrange
            DbContextOptions <YellowJacketContext> options = new DbContextOptionsBuilder <YellowJacketContext>()
                                                             .UseInMemoryDatabase("AddAgent_ValidAgent_NoError")
                                                             .Options;

            const string agentName = "MyAgent";

            // Act
            using (YellowJacketContext context = new YellowJacketContext(options))
            {
                IAgentRepository agentRepository = new AgentRepository(context);

                IAgentService service = new AgentService(agentRepository, GetMapper());

                AgentModel model = new AgentModel
                {
                    Name         = agentName,
                    LastUpdateOn = DateTime.Now,
                    RegisteredOn = DateTime.Now,
                    Status       = AgentStatus.Idle.ToString()
                };

                await service.Add(model);
            }

            // Assert
            using (YellowJacketContext context = new YellowJacketContext(options))
            {
                const int expectedCount = 1;

                Assert.That(
                    expectedCount, Is.EqualTo(Convert.ToInt32(context.Agents.Count())),
                    $"The agents count should be {expectedCount}.");

                Assert.That(
                    agentName,
                    Is.EqualTo(context.Agents.Single().Name),
                    $"The expected agent name {agentName} doesn't match the actual value {context.Agents.Single().Name}");
            }
        }