コード例 #1
0
        public void GetDetail_Mocks_ForDebug()
        {
            // Arrange
            var employeeProject = new EmployeeProject
            {
                Id                = 1,
                EmployeeId        = 2,
                ProjectId         = 3,
                ValidityBeginDate = DateTime.Today.AddDays(-1),
                ValidityEndDate   = DateTime.Today.AddDays(1)
            };

            var employee = new Employee
            {
                Id = 2,
                EmployeeProjects = new List <EmployeeProject> {
                    employeeProject
                }
            };

            Func <IFilteredQuery <EmployeeProjectDTO, EmployeeProjectFilterDTO> > queryFactory = () => new Mock <IFilteredQuery <EmployeeProjectDTO, EmployeeProjectFilterDTO> >().Object;
            var entityMapper    = new Mock <IEntityDTOMapper <EmployeeProject, EmployeeProjectDTO> >().Object;
            var respositoryMock = new Mock <IRepository <EmployeeProject, int> >();

            respositoryMock.Setup(r => r.GetById(It.IsAny <int>(), It.IsAny <Expression <Func <EmployeeProject, object> >[]>())).Returns <int, Expression <Func <EmployeeProject, object> >[]>((id, x) => employeeProject);
            var parentRepositoryMock = new Mock <IRepository <Employee, int> >();

            parentRepositoryMock.Setup(r => r.GetById(It.IsAny <int>(), It.IsAny <Expression <Func <Employee, object> >[]>())).Returns <int, Expression <Func <Employee, object> >[]> ((id, x) => employee);
            var dateTimeProvider       = new LocalDateTimeProvider();
            var unitOfWorkProviderMock = new Mock <IUnitOfWorkProvider>();

            unitOfWorkProviderMock.Setup(p => p.Create()).Returns(() => new Mock <IUnitOfWork>().Object);
            var facade = new EmployeeProjectsFacade(queryFactory, entityMapper, respositoryMock.Object, parentRepositoryMock.Object, dateTimeProvider, unitOfWorkProviderMock.Object);

            // Act - only for debug
            facade.GetDetail(1);
        }
コード例 #2
0
        private static void PrepareInMemoryDbContext(DbContextOptions <EmployeeProjectDbContext> options)
        {
            // Employee  Project  EmployeeProject
            //        1       10              100
            //        2       10              101
            //        2       11              102
            //        9       99              901
            //        9       99              902
            //        9       99              903

            var employeeProject100 = new EmployeeProject
            {
                Id                = 100,
                EmployeeId        = 1,
                ProjectId         = 10,
                ValidityBeginDate = DateTime.Today.AddDays(-1),
                ValidityEndDate   = DateTime.Today.AddDays(1)
            };

            var employeeProject101 = new EmployeeProject
            {
                Id                = 101,
                EmployeeId        = 2,
                ProjectId         = 10,
                ValidityBeginDate = DateTime.Today.AddDays(-1),
                ValidityEndDate   = DateTime.Today.AddDays(1)
            };

            var employeeProject102 = new EmployeeProject
            {
                Id                = 102,
                EmployeeId        = 2,
                ProjectId         = 11,
                ValidityBeginDate = DateTime.Today.AddDays(-1),
                ValidityEndDate   = DateTime.Today.AddDays(1)
            };

            var employeeProject901 = new EmployeeProject
            {
                Id                = 901,
                EmployeeId        = 9,
                ProjectId         = 99,
                ValidityBeginDate = null,
                ValidityEndDate   = DateTime.Today.AddDays(-1).AddHours(-2)
            };

            var employeeProject902 = new EmployeeProject
            {
                Id                = 902,
                EmployeeId        = 9,
                ProjectId         = 99,
                ValidityBeginDate = DateTime.Today.AddDays(-1).AddHours(-2),
                ValidityEndDate   = DateTime.Today.AddDays(-1).AddHours(-1)
            };

            var employeeProject903 = new EmployeeProject
            {
                Id                = 903,
                EmployeeId        = 9,
                ProjectId         = 99,
                ValidityBeginDate = DateTime.Today.AddDays(-1).AddHours(-1),
                ValidityEndDate   = null
            };

            var employees = new List <Employee>
            {
                new Employee
                {
                    Id = 1,
                    EmployeeProjects = new List <EmployeeProject> {
                        employeeProject100
                    }
                },
                new Employee
                {
                    Id = 2,
                    EmployeeProjects = new List <EmployeeProject> {
                        employeeProject101, employeeProject102
                    }
                },
                new Employee
                {
                    Id = 9,
                    EmployeeProjects = new List <EmployeeProject> {
                        employeeProject901, employeeProject902, employeeProject903
                    }
                }
            };

            var projects = new List <Project>
            {
                new Project
                {
                    Id = 10,
                    EmployeeProjects = new List <EmployeeProject> {
                        employeeProject100, employeeProject101
                    }
                },
                new Project
                {
                    Id = 11,
                    EmployeeProjects = new List <EmployeeProject> {
                        employeeProject102
                    }
                },
                new Project
                {
                    Id = 99,
                    EmployeeProjects = new List <EmployeeProject> {
                        employeeProject901, employeeProject902, employeeProject903
                    }
                }
            };

            using (var ctx = new EmployeeProjectDbContext(options))
            {
                ctx.Employees.AddRange(employees);
                ctx.Projects.AddRange(projects);
                ctx.EmployeeProjects.Add(employeeProject100);
                ctx.EmployeeProjects.Add(employeeProject101);
                ctx.EmployeeProjects.Add(employeeProject102);
                ctx.EmployeeProjects.Add(employeeProject901);
                ctx.EmployeeProjects.Add(employeeProject902);
                ctx.EmployeeProjects.Add(employeeProject903);
                ctx.SaveChanges();
            }
        }