EF linq模拟中的临时创建表中的异常

时间:2018-09-12 06:12:21

标签: c# entity-framework linq unit-testing mocking

我已经用Fake DB Context实现了EF测试。以下是我的sql查询,

     flatLicenseOrderId = (from users in this.CustomerEntities.UserMappers
                                              where users.CompanyId == CompanyId && users.IsActive
                                              join order in this.CustomerEntities.Orders on users.customerid equals order.customerid into orderDetails
                                              from customerOrder in orderDetails.DefaultIfEmpty()
                                              join license in this.CustomerEntities.License on users.Customerid equals license.Customerid into licenseDetails
                                              from customerLicense in licenseDetails.DefaultIfEmpty()
                                              from orderdetails in this.CustomerEntities.OrderDetails
                                              where (customerOrder.OrderId == orderdetails.OrderId || customerLicense.OrderId == orderdetails.OrderId) 
                                              select orderdetails.OrderId).Distinct().ToList();

下面是我的模拟内容

     public static CustomerEntities GetOrderDetails()
            {
                var mockData = new Mock<CustomerEntities>();                

                var users = new FakeDbSet<UserMapper>
                {
                    new UserMapper { CompanyId = 27835, IsActive = true, CustomerId = 956980 },
                    new UserMapper { CompanyId = 148150, IsActive = true, CustomerId = 1039733 }
                };

                mockData.Setup(m => m.UserMappers).Returns(users);

                var order = new FakeDbSet<Orders>
                {
                    new Orders { CustomerId  = 956980, OrderId = 401789 },
                    new Orders { CustomerId = 956980, OrderId = 426192 }                   
                };

                mockData.Setup(m => m.Orders).Returns(order);    

                var license = new FakeDbSet<License>
                {
                   new License { Customerid = 956980, OrderId = 401789 },
                   new License { Customerid = 1039733, OrderId = 423132 }
                };

                mockData.Setup(m => m.License).Returns(license);

                var orderdetails = new FakeDbSet<OrderDetails>
                {
                    new OrderDetails { OrderId = 401789, ProductID = 7703 },
                    new OrderDetails { OrderId = 401789, ProductID = 7002 }                   
                };

                mockData.Setup(m => m.OrderDetails).Returns(orderdetails);

                return mockData.Object;
            }

虽然使用所有使用的表中存在的值来模拟此代码,但该模拟工作正常。但是,如果该值不在“订单”表中,则出现以下错误,

Message: Object reference not set to an instance of an object.

Source: Anonymously Hosted DynamicMethods Assembly

示例:对于customerid = 956980,该模拟工作正常,但是对于另一个customerid = 1039733,则产生异常,因为该客户在Order表中没有条目。

有人可以帮忙,如何处理此类情况下的嘲讽?

1 个答案:

答案 0 :(得分:-1)

尝试一下。 在数据库操作的情况下,它可能会失败,但是由于concise操作,对于样机它就可以了。

flatLicenseOrderId = (from users in this.CustomerEntities.UserMappers
                                        .Where(x=>x.CompanyId==CompanyId && x.IsActive)
                      join order in this.CustomerEntities.Orders 
                           on users.customerid equals order.customerid 
                      into orderDetails
                      from customerOrder in orderDetails.DefaultIfEmpty()
                      join orderdetails in this.CustomerEntities.OrderDetails
                      on customerOrder?.OrderId equals orderdetails.OrderId
                      select new{users.customerid,orderdetails.OrderId})
                     .Distinct().ToList();