我正在使用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);
}
非常感谢任何帮助。
答案 0 :(得分:2)
尝试添加:
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);
}
然后您就可以轻松地使用数据访问层了。