Example #1
0
        public static void Destroy(ReturnDbContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            context.Database.EnsureDeleted();

            context.Dispose();
        }
        protected override void ConfigureWebHost(IWebHostBuilder builder)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            // Find free TCP port to configure Kestel on
            IPEndPoint endPoint;

            using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
                socket.Bind(new IPEndPoint(IPAddress.Loopback, 0));
                socket.Listen(1);
                endPoint = (IPEndPoint)socket.LocalEndPoint;
            }

            // Configure testing to use Kestel and test services
            builder
            .ConfigureLogging(lb => {
                lb.SetMinimumLevel(LogLevel.Trace);
                lb.AddProvider(new TestContextLoggerProvider());

                string logFileName = (TestContext.CurrentContext?.Test.ClassName ?? "test-log") + ".log";
                lb.AddFile(Path.Join(Paths.TestArtifactDir, logFileName));
            })
            .ConfigureTestServices(services => {
                // Add a database context using an in-memory database for testing.
                services.RemoveAll <ReturnDbContext>();

                services.AddScoped(sp => {
                    DbContextOptions <ReturnDbContext> options = new DbContextOptionsBuilder <ReturnDbContext>()
                                                                 .UseSqlite(this.ConnectionString)
                                                                 .Options;

                    var context = new ReturnDbContext(options);

                    return(context);
                });
                services.ChainInterfaceImplementation <IReturnDbContext, ReturnDbContext>();
                services.ChainInterfaceImplementation <IReturnDbContextFactory, ReturnDbContext>();

                services.Configure <ServerOptions>(s => {
                    s.BaseUrl = "http://localhost:" + endPoint.Port + "/";
                });
            })
            .UseKestrel(k => k.Listen(endPoint))
            .UseEnvironment(environment: "Test");
        }
Example #3
0
        public static ReturnDbContext Create()
        {
            DbContextOptions <ReturnDbContext> options = new DbContextOptionsBuilder <ReturnDbContext>()
                                                         .UseInMemoryDatabase(Guid.NewGuid().ToString())
                                                         .Options;

            var context = new ReturnDbContext(options);

            context.Database.EnsureCreated();

            new SeedBaseDataCommandHandler(context).Handle(new SeedBaseDataCommand(), CancellationToken.None).
            ConfigureAwait(false).
            GetAwaiter().
            GetResult();

            context.SaveChanges();

            return(context);
        }
Example #4
0
 public CommandTestBase()
 {
     this.Context = ReturnDbContextFactory.Create();
 }