Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET样板文件,带有真实数据库的测试用例_C#_Integration Testing_Aspnetboilerplate - Fatal编程技术网

C# ASP.NET样板文件,带有真实数据库的测试用例

C# ASP.NET样板文件,带有真实数据库的测试用例,c#,integration-testing,aspnetboilerplate,C#,Integration Testing,Aspnetboilerplate,我正在为asp.net 5.x项目使用asp.net样板文件。我正在尝试为这个项目创建测试用例,它将调用现有的测试数据库(一个数据库用于主机,另一个数据库用于租户)。到目前为止,我的步骤是: 在TestBase类构造函数中,我调用的方法与MultiTenantMigrateExecuter.Run()的工作原理相同,该方法将为测试主机数据库和测试租户数据库(我将使用主机数据库和单租户数据库进行测试)播种数据。种子设定与实际数据库相同,只是测试数据库的名称不同 同样从TestBase类构造函数中,

我正在为asp.net 5.x项目使用asp.net样板文件。我正在尝试为这个项目创建测试用例,它将调用现有的测试数据库(一个数据库用于主机,另一个数据库用于租户)。到目前为止,我的步骤是:

  • TestBase
    类构造函数中,我调用的方法与
    MultiTenantMigrateExecuter.Run()的工作原理相同,该方法将为测试主机数据库和测试租户数据库(我将使用主机数据库和单租户数据库进行测试)播种数据。种子设定与实际数据库相同,只是测试数据库的名称不同
  • 同样从
    TestBase
    类构造函数中,我从主机数据库中获取租户ID
  • 接下来,我将尝试从租户数据库中获取任何种子用户 如下所示:
    var user=UsingDbContext(context=>context.Users.FirstOrDefault(t=>t.UserName==“johndoe”)但是当然这将调用HostDb而不是TenantDb
  • 我找到了一种调用TenantDb的方法,方法是将代码包装在这样的using语句中,避免使用
    上下文
    ,并使用存储库,以便能够从TenantDb获取我需要的用户:

    using (this.AbpSession.Use(tenant.Id, null))
    {
        // get the TenantDb.User here by using the User repository
    }
    
    。。。然后在我写的每个测试用例中都是这样:

    using (this.AbpSession.Use(AbpSession.TenantId, AbpSession.UserId))
    {
        // make calls to the Tenant database here by using Tenant repository
    }
    
    但这并不是最干净的解决方案,也有其局限性

    问题是:在我的例子中,有没有更好的方法,在
    TestBase
    类中设置上下文,以便在默认情况下调用租户数据库而不是主机数据库

    我也试过这个,但没用

    protected T UsingTenantDbContext<T>(Func<TestAppDbContext, T> func)
    {
        T result;
    
        using (this.AbpSession.Use(AbpSession.TenantId, AbpSession.UserId))
        {
            using (var context = LocalIocManager.Resolve<TestAppDbContext>())
            {
                context.DisableAllFilters();
                result = func(context);
                context.SaveChanges();
            }
        }
    
        return result;
    }
    
    使用TenantDBContext(Func-Func)受保护的T
    {
    T结果;
    使用(this.AbpSession.Use(AbpSession.TenantId,AbpSession.UserId))
    {
    使用(var context=LocalIocManager.Resolve())
    {
    context.DisableAllFilters();
    结果=func(上下文);
    SaveChanges();
    }
    }
    返回结果;
    }
    
    玩了一会儿代码后,我找到了问题的答案。。。 在TestBase类中,我创建了一个新的静态属性:

    internal static MyAppDbContext tenantContext;
    
    静态,因为该类将被多次继承,但tenantContext只应设置一次

    接下来,我创建了以下方法:

    protected void CreateTenantDbContext()
    {
        if (tenantContext == null)
        {
            using (var context = LocalIocManager.Resolve<MyAppDbContext>())
            {
                // AbpSession.TenantId is set in a previous method.
                // Usin the host context, get the Connection String for the necessary tenant
                var encryptedDbConnString = context.Tenants.FirstOrDefault(x => x.Id == AbpSession.TenantId)?.ConnectionString;
    
                // Decrypt the string
                var decryptedDbConnString = SimpleStringCipher.Instance.Decrypt(encryptedDbConnString);
    
                // Create the context for the tenant db and assign it to the static property tenantContext
                tenantContext = LocalIocManager.Resolve<MyAppDbContext>(new { nameOrConnectionString = decryptedDbConnString });
            }
        }
    }
    
    protectedvoid CreateTenantDbContext()受保护
    {
    if(租户上下文==null)
    {
    使用(var context=LocalIocManager.Resolve())
    {
    //AbpSession.TenantId是在前面的方法中设置的。
    //在主机上下文中,获取必要租户的连接字符串
    var encryptedbconnstring=context.Tenants.FirstOrDefault(x=>x.Id==AbpSession.TenantId)?.ConnectionString;
    //解密字符串
    var decryptedDbConnString=SimpleStringCipher.Instance.Decrypt(encryptedDbConnString);
    //为租户数据库创建上下文,并将其分配给静态属性tenantContext
    tenantContext=LocalIocManager.Resolve(新的{nameOrConnectionString=decryptedDbConnString});
    }
    }
    }
    
    一旦创建,您就可以在测试用例中使用它