Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mock OData Client's Container using Moq

I am using OData V4 client to create proxy inside my asp.net mvc 5. I want to unit test the controllers using Moq. Is there any way I can mock the OData service response by container. Below is the OData container instantiator:

    public static class ControlEntityContextHelper
    {
         /// <summary>
         /// Returns OData service context
         /// </summary>
         /// <returns></returns>
         public static Container GetEntityContext()
         {
             // create the container
             var container = new Container(new Uri("http://localhost/services/odata/"));
             container.Timeout = 1800;
             return container;
          }
     } 

Below is the MVC Controller:

    public JsonResult GetEmployees(string employeeId)
    {
        var odataContext = ControlEntityContextHelper.GetEntityContext();
        var employee = odataContext.Employees.Where(emp => emp.EmployeeId == employeeId);
        return Json(employee, JsonRequestBehavior.AllowGet);
    }

Any help will be greatly appreciated.

like image 326
gee'K'iran Avatar asked Nov 17 '15 09:11

gee'K'iran


1 Answers

Try to add this:

public interface IEmployeeRepository
{
    Employee GetById(string id);
}

public class EmployeeRepository: IEmployeeRepository
{
    public Employee GetById() {//access to OData}
} 

And then change your controller to

public JsonResult GetEmployees(string employeeId)
  {
        var employee = employeeRepository.GetById(employeeId);
        return Json(employee, JsonRequestBehavior.AllowGet);
  }

Then you will be able to easily your Data Access Layer.

like image 185
skalinkin Avatar answered Oct 23 '22 04:10

skalinkin