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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With