Exception in Temporary created table in EF linq mocking

Viewed 91

I have implemented EF Testing with Fake DB Context. The below is my sql query,

     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();

Below is my Mocking context,

     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;
            }

While mocking this code with value that present in all the tables used, the mocking works fine. But if the value is not in Orders table, am getting the following error,

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

Source: Anonymously Hosted DynamicMethods Assembly

Example: for customerid = 956980, the mocking works fine, but for the another customerid = 1039733 , produces exception, since the customer does not have entry in Order table.

Can someone help, how to handle mocking for these kind of cases?

1 Answers

Try this. It may fail in case of database operations but for mockups it will do just fine because of concise operations.

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();
Related