Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.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# 使用Moq模拟包含导航属性的EntityFramework查询_C#_Entity Framework_Unit Testing_Moq - Fatal编程技术网

C# 使用Moq模拟包含导航属性的EntityFramework查询

C# 使用Moq模拟包含导航属性的EntityFramework查询,c#,entity-framework,unit-testing,moq,C#,Entity Framework,Unit Testing,Moq,我正在尝试为我们的存储库/工作单元框架设置单元测试和集成测试。在引入导航属性之前,模拟查询可以正常工作。以下工作很好: var customers = (from cus in Customers.GetAll() join acm in AccountManagers.GetAll() on cus.INS_Id equals acm.INS_Id join the in Employees.GetAll() on

我正在尝试为我们的存储库/工作单元框架设置单元测试和集成测试。在引入导航属性之前,模拟查询可以正常工作。以下工作很好:

var customers = (from cus in Customers.GetAll()
                    join acm in AccountManagers.GetAll() on cus.INS_Id equals acm.INS_Id
                    join the in Employees.GetAll() on acm.THE_Id equals the.THE_Id

                    select new CustomerModel()
                    {
                        Id = cus.INS_Id,
                        Name = cus.INS_Name,
                        OrganizationNumber = cus.INS_OrgNo,
                        PostalAddress = cus.INS_PostalAddress1,
                        PostalCity = cus.INS_PostalCity,
                        ...
但是,向select中添加这样的行将不起作用:

var customers = (from cus in Customers.GetAll()
                    join acm in AccountManagers.GetAll() on cus.INS_Id equals acm.INS_Id
                    join the in Employees.GetAll() on acm.THE_Id equals the.THE_Id

                    select new CustomerModel()
                    {
                        Id = cus.INS_Id,
                        Name = cus.INS_Name,
                        OrganizationNumber = cus.INS_OrgNo,
                        PostalAddress = cus.INS_PostalAddress1,
                        PostalCity = cus.INS_PostalCity,
                        Brokered = cus.InsuredBrokers.Any(),
                        ...
原因是
cus.InsuredBrokers
是一个导航属性,Moq不知道如何处理它

我设置了如下测试:

// Sets up a new Mock DbSet, given a list of inmemory data
private Mock<DbSet<T>> SetupSet<T>(IQueryable<T> data) where T : class
{
    var mockSet = new Mock<DbSet<T>>();
    mockSet.As<IQueryable<T>>().Setup(m => m.Provider).Returns(data.Provider);
    mockSet.As<IQueryable<T>>().Setup(m => m.Expression).Returns(data.Expression);
    mockSet.As<IQueryable<T>>().Setup(m => m.ElementType).Returns(data.ElementType);
    mockSet.As<IQueryable<T>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());

    return mockSet;
}

// Sets up a new Mock DbContext with a given Mock DbSet of data
private Mock<PrositContext> SetupContext<T>(Mock<DbSet<T>> set) where T : class
{
    var mockContext = new Mock<PrositContext>();
    mockContext.Setup(m => m.Set<T>()).Returns(set.Object);

    return mockContext;
}

// Adds a given Mock DbSet of data to an existing Mock DbContext
private Mock<PrositContext> SetupContext<T>(Mock<DbSet<T>> set, Mock<PrositContext> existingContext) where T : class
{
    existingContext.Setup(m => m.Set<T>()).Returns(set.Object);

    return existingContext;
}

// Sets up a new Mock DbContext given a list of inmemory data
private Mock<PrositContext> SetupContext<T>(IQueryable<T> data) where T : class
{
    var mockSet = SetupSet(data);
    return SetupContext(mockSet);
}

// Adds to an existing Mock DbContext a list of inmemory data
private Mock<PrositContext> SetupContext<T>(IQueryable<T> data, Mock<PrositContext> existingContext) where T : class
{
    var mockSet = SetupSet(data);
    return SetupContext(mockSet, existingContext);
}

[TestMethod]
public void TestGetCustomerName()
{
    // ARRANGE

    // Read the data from a .csv file
    var customerData = new CsvCustomerRepository(null).GetAll();
    var accountManagerData = new CsvAccountManagerRepository(null).GetAll();
    var employeeData = new CsvTHEmployeeRepository(null).GetAll();
    var nrsData = new CsvNordicRiskSolutionsRepository(null).GetAll();
    var inbData = new CsvInsuredBrokersRepository(null).GetAll();
    var gsiData = new CsvGeneralSystemInfoRepository(null).GetAll();

    // Setup the mock context with all the data from the .csv
    var mockContext = SetupContext(customerData);
    SetupContext(accountManagerData, mockContext);
    SetupContext(employeeData, mockContext);
    SetupContext(nrsData, mockContext);
    SetupContext(inbData, mockContext);
    SetupContext(gsiData, mockContext);

    var customerFacade = new CustomerFacade(mockContext.Object);

    // ACT

    var customers = customerFacade.GetByNameMOCKFORTESTING("Bos");

    // ASSERT

    //Assert.AreEqual(...);
}

public IEnumerable<ICustomerModel> GetByNameMOCKFORTESTING(string name)
{
    var customers = (from cus in Customers.GetAll()
                        join acm in AccountManagers.GetAll() on cus.INS_Id equals acm.INS_Id
                        join the in Employees.GetAll() on acm.THE_Id equals the.THE_Id
                        where cus.INS_Name.Contains(name)
                        select new CustomerModel()
                        {
                            Id = cus.INS_Id,
                            Name = cus.INS_Name,
                            OrganizationNumber = cus.INS_OrgNo,
                            PostalAddress = cus.INS_PostalAddress1,
                            PostalCity = cus.INS_PostalCity,
                            Brokered = cus.InsuredBrokers.Any(),
                            CustomerResponsible = new THEmployeeModel()
                            {
                                FirstName = the.THE_FirstName,
                                LastName = the.THE_LastName,
                                PhoneNumber = null
                            },
                            RiskClass = cus.INS_RiskKlass,
                        }).Take(300).ToList();

    return customers;
}
//在给定内存数据列表的情况下,设置一个新的模拟数据库集
私有模拟设置集(IQueryable数据),其中T:class
{
var mockSet=new Mock();
mockSet.As().Setup(m=>m.Provider).返回(data.Provider);
mockSet.As().Setup(m=>m.Expression).Returns(data.Expression);
mockSet.As().Setup(m=>m.ElementType).Returns(data.ElementType);
mockSet.As().Setup(m=>m.GetEnumerator()).Returns(data.GetEnumerator());
返回模拟集;
}
//使用给定的模拟数据集设置新的模拟DbContext
私有模拟SetupContext(模拟集),其中T:class
{
var mockContext=new Mock();
mockContext.Setup(m=>m.Set()).Returns(Set.Object);
返回上下文;
}
//将给定的模拟数据库集数据添加到现有的模拟数据库上下文
私有模拟SetupContext(模拟集,模拟现有上下文),其中T:class
{
existingContext.Setup(m=>m.Set()).Returns(Set.Object);
返回现有上下文;
}
//在给定inmemory数据列表的情况下,设置新的模拟DbContext
私有模拟SetupContext(IQueryable数据),其中T:class
{
var mockSet=设置集(数据);
返回SetupContext(mockSet);
}
//向现有的模拟DbContext添加内存数据列表
私有模拟SetupContext(IQueryable数据,模拟现有上下文),其中T:class
{
var mockSet=设置集(数据);
返回SetupContext(mockSet,existingContext);
}
[测试方法]
public void TestGetCustomerName()
{
//安排
//从.csv文件中读取数据
var customerData=new CsvCustomerRepository(null).GetAll();
var accountManagerData=new CsvAccountManagerRepository(null).GetAll();
var employeeData=new CsvTHEmployeeRepository(null).GetAll();
var nrsData=new CsvNordicRiskSolutionsRepository(null).GetAll();
var inbData=new CsvInsuredBrokersRepository(null).GetAll();
var gsiData=new CsvGeneralSystemInfoRepository(null).GetAll();
//使用.csv文件中的所有数据设置模拟上下文
var mockContext=SetupContext(customerData);
SetupContext(accountManagerData、mockContext);
SetupContext(employeeData、mockContext);
SetupContext(nrsData、mockContext);
SetupContext(inbData、mockContext);
SetupContext(gsiData、mockContext);
var customerFacade=新的customerFacade(mockContext.Object);
//表演
var customers=customerFacade.GetByNameMOCKFORTESTING(“Bos”);
//断言
//断言。AreEqual(…);
}
公共IEnumerable GetByNameMOCKFORTESTING(字符串名称)
{
var customers=(来自customers.GetAll()中的cus)
在AccountManagers中加入acm。cus.INS\u Id上的GetAll()等于acm.INS\u Id
加入acm上的Employees.GetAll()中。该\u Id等于
其中cus.INS\u Name.Contains(Name)
选择新CustomerModel()
{
Id=cus.INS\u Id,
Name=cus.INS\u Name,
OrganizationNumber=cus.INS\u OrgNo,
PostalAddress=cus.INS\u PostalAddress 1,
PostalCity=cus.INS\u PostalCity,
Brokered=cus.insuraredBrokers.Any(),
CustomerResponsible=new themployeModel()
{
FirstName=the.the_FirstName,
LastName=the.the_LastName,
PhoneNumber=null
},
风险等级=cus.INS\u risklass,
}).Take(300.ToList();
返回客户;
}

我需要做什么来模拟客户保险经纪人?我发现的示例、视频和教程没有涉及导航属性的主题,即使它们是常用的…

模仿实体框架是相当困难的。即使您成功地模拟了导航属性,那么使用延迟加载的序列或代码呢。最终,您将为测试编写大型模拟设置

我们遇到了一个类似的问题,并使用内存数据库的EF提供程序解决了这个问题。您可以为测试设置数据库,只需模拟工作单元的创建