Exemplo n.º 1
0
        public static void Setup(TestContext context)
        {
            var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json");

            _configuration = builder.Build();

            EntityFrameworkHelper.CreateSchema();

            DependencyInjector.Kernel.StartEntityFramework();

            var kernel = Kernel.GetKernel();

            kernel.Register <DbContext>(() =>
            {
                var opBuilder = new DbContextOptionsBuilder <SharedKernelTestContext>();
                opBuilder.UseSqlServer(_configuration.GetConnectionString("SharedKernel.Test"));
                return(new SharedKernelTestContext(opBuilder.Options));
            });

            _helperRepository = DependencyInjector.Kernel.Get <IHelperRepository>();

            using (var session = _helperRepository.OpenSession())
            {
                var repo = session.GetRepository <Foo>();
                try
                {
                    session.StartTransaction();

                    var foo1 = new Foo {
                        Bar = 10, DataInclusao = DateTime.Now, UsuarioInclusao = "test"
                    };
                    var foo2 = new Foo {
                        Bar = 20, DataInclusao = DateTime.Now, UsuarioInclusao = "test"
                    };
                    var foo3 = new Foo {
                        Bar = 30, DataInclusao = DateTime.Now, UsuarioInclusao = "test"
                    };

                    repo.Insert(foo1);
                    repo.Insert(foo2);
                    repo.Insert(foo3);

                    session.CommitTransaction();
                }
                catch (Exception ex)
                {
                    session.RollBackTransaction();
                    Console.WriteLine(ex.Message);
                }
            }
        }