gee'K'iran
gee'K'iran

Reputation: 441

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.

Upvotes: 10

Views: 1543

Answers (1)

skalinkin
skalinkin

Reputation: 1064

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.

Upvotes: 2

Related Questions