Ejemplo n.º 1
0
        public void Init()
        {
            var serviceCollection = GetService();

            serviceCollection.AddMemoryCache();
            serviceCollection.AddOptions();

            var optionMock = new Mock <IOptions <AppSetting> >();

            optionMock.Setup(x => x.Value).Returns(new AppSetting {
                DbType = Define.DBTYPE_MYSQL
            });
            serviceCollection.AddScoped(x => optionMock.Object);

            // 测试my sql
            serviceCollection.AddDbContext <OpenAuthDBContext>(options =>
                                                               options.UseMySql("server=127.0.0.1;user id=root;database=openauthdb;password=000000"));

//            serviceCollection.AddDbContext<OpenAuthDBContext>(options =>
//                options.UseSqlServer("Data Source=.;Initial Catalog=OpenAuthDB;User=sa;Password=000000;Integrated Security=True"));

            var container = AutofacExt.InitForTest(serviceCollection);

            _autofacServiceProvider = new AutofacServiceProvider(container);
        }
Ejemplo n.º 2
0
        public void Init()
        {
            var serviceCollection = GetService();

            serviceCollection.AddMemoryCache();
            serviceCollection.AddOptions();
            //读取OpenAuth.WebApi的配置文件用于单元测试
            var            path     = AppContext.BaseDirectory;
            int            pos      = path.IndexOf("OpenAuth.App");
            var            basepath = Path.Combine(path.Substring(0, pos), "OpenAuth.WebApi");
            IConfiguration config   = new ConfigurationBuilder()
                                      .SetBasePath(basepath)
                                      .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                                      .AddJsonFile("appsettings.Development.json", optional: true)
                                      .AddEnvironmentVariables()
                                      .Build();

            serviceCollection.Configure <AppSetting>(config.GetSection("AppSetting"));
            //添加log4net
            serviceCollection.AddLogging(builder =>
            {
                builder.ClearProviders();                               //去掉默认的日志
                builder.AddConfiguration(config.GetSection("Logging")); //读取配置文件中的Logging配置
                builder.AddLog4Net();
            });
            //注入OpenAuth.WebApi配置文件
            serviceCollection.AddScoped(x => config);

            //模拟HTTP请求
            var httpContextAccessorMock = new Mock <IHttpContextAccessor>();

            httpContextAccessorMock.Setup(x => x.HttpContext.Request.Query[Define.TOKEN_NAME]).Returns("tokentest");
            httpContextAccessorMock.Setup(x => x.HttpContext.Request.Query[Define.TENANT_ID]).Returns("OpenAuthDBContext");
            serviceCollection.AddScoped(x => httpContextAccessorMock.Object);

            serviceCollection.AddDbContext <OpenAuthDBContext>();

            var container = AutofacExt.InitForTest(serviceCollection);

            _autofacServiceProvider = new AutofacServiceProvider(container);
            AutofacContainerModule.ConfigServiceProvider(_autofacServiceProvider);

            var dbtypes = config.GetSection("AppSetting:DbTypes").GetChildren()
                          .ToDictionary(x => x.Key, x => x.Value);

            Console.WriteLine($"单元测试数据库信息:{dbtypes[httpContextAccessorMock.Object.GetTenantId()]}/{config.GetSection("ConnectionStrings")["OpenAuthDBContext"]}");
        }
Ejemplo n.º 3
0
        public void Init()
        {
            var serviceCollection = GetService();

            serviceCollection.AddMemoryCache();
            serviceCollection.AddOptions();

            // 测试my sql
            // serviceCollection.AddDbContext<OpenAuthDBContext>(options =>
            //     options.UseMySql("server=127.0.0.1;user id=root;database=openauthdb;password=000000"));

            serviceCollection.AddDbContext <OpenAuthDBContext>(options =>
                                                               options.UseSqlServer("Data Source=.;Initial Catalog=OpenAuthDB;User=sa;Password=000000;Integrated Security=True"));

            var container = AutofacExt.InitForTest(serviceCollection);

            _autofacServiceProvider = new AutofacServiceProvider(container);
        }
Ejemplo n.º 4
0
        public void Init()
        {
            var serviceCollection = GetService();

            serviceCollection.AddMemoryCache();
            serviceCollection.AddOptions();
            //讀取OpenAuth.WebApi的配置檔案用於單元測試
            var            path     = AppContext.BaseDirectory;
            int            pos      = path.IndexOf("OpenAuth.App");
            var            basepath = Path.Combine(path.Substring(0, pos), "OpenAuth.WebApi");
            IConfiguration config   = new ConfigurationBuilder()
                                      .SetBasePath(basepath)
                                      .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                                      .AddJsonFile("appsettings.Development.json", optional: true)
                                      .AddEnvironmentVariables()
                                      .Build();

            Console.WriteLine($"單元測試資料庫資訊:{config.GetSection("AppSetting")["DbType"]}/{config.GetSection("ConnectionStrings")["OpenAuthDBContext"]}");

            //新增log4net
            serviceCollection.AddLogging(builder =>
            {
                builder.ClearProviders();                               //去掉預設的日誌
                builder.AddConfiguration(config.GetSection("Logging")); //讀取配置檔案中的Logging配置
                builder.AddLog4Net();
            });
            //注入OpenAuth.WebApi配置檔案
            serviceCollection.AddScoped(x => config);

            //模擬HTTP請求
            var httpContextAccessorMock = new Mock <IHttpContextAccessor>();

            httpContextAccessorMock.Setup(x => x.HttpContext.Request.Query[Define.TOKEN_NAME]).Returns("tokentest");
            httpContextAccessorMock.Setup(x => x.HttpContext.Request.Query[Define.TENANT_ID]).Returns("OpenAuthDBContext");
            serviceCollection.AddScoped(x => httpContextAccessorMock.Object);

            serviceCollection.AddDbContext <OpenAuthDBContext>();

            var container = AutofacExt.InitForTest(serviceCollection);

            _autofacServiceProvider = new AutofacServiceProvider(container);
            AutofacContainerModule.ConfigServiceProvider(_autofacServiceProvider);
        }