简体   繁体   English

使用Moq模拟OData客户端的容器

[英]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. 我正在使用OData V4客户端在我的asp.net mvc 5中创建代理。我想使用Moq对控制器进行单元测试。 Is there any way I can mock the OData service response by container. 有没有办法可以通过容器模拟OData服务响应。 Below is the OData container instantiator: 下面是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;
          }
     } 

Below is the MVC Controller: 下面是MVC控制器:

    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. 然后,您将能够轻松地进行数据访问层。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM