Search code examples
.netunit-testingc#-4.0microsoft-fakes

How to shim using statement?


I have a method similar to the following:

public List<EmployeeReports> MyMethod (int empId, DateTime startDate)
{
  using(SomeEntities se = new SomeEntities())
  {
   List<EmployeeReports> reports = se.EmployeeReports
   .Where(x => x.EmployeeId == empId
   && DateTime.Compare(x.DateEntered, startDate).ToList();
  }

 return reports;
}

This is an entity framework object to a database.

How can the using block be shimmed?


Solution

  • stop using the new keyword in any code. If this SomeEntities is of utmost necessity pass it to the constructor of the class (constructor Injection). If SomeEntities needs to be overriden and assignment is no the prime requirement for creating the object of the class and ensure that SomeEntities are set before the function call (Property Injection).