我正在使用OData V4客户端在我的asp.net mvc 5中创建代理.我想使用Moq对控制器进行单元测试.有什么办法可以通过容器来模拟OData服务响应.以下是OData容器实例化器:
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; } }
下面是MVC控制器:
public JsonResult GetEmployees(string employeeId) { var odataContext = ControlEntityContextHelper.GetEntityContext(); var employee = odataContext.Employees.Where(emp => emp.EmployeeId == employeeId); return Json(employee,JsonRequestBehavior.AllowGet); }
任何帮助将不胜感激.
解决方法
尝试添加:
public interface IEmployeeRepository { Employee GetById(string id); } public class EmployeeRepository: IEmployeeRepository { public Employee GetById() {//access to OData} }
然后将您的控制器更改为
public JsonResult GetEmployees(string employeeId) { var employee = employeeRepository.GetById(employeeId); return Json(employee,JsonRequestBehavior.AllowGet); }
那么您将能够轻松地进行数据访问层.