2015-11-17 15 views
10

Sto usando il client OData V4 per creare il proxy all'interno del mio asp.net mvc 5. Voglio testare i controller usando Moq. C'è un modo in cui posso prendere in giro la risposta del servizio OData per container. Di seguito l'OData contenitore instantiator:Mock OData Contenitore del client usando Moq

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; 
      } 
    } 

sotto è il 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); 
    } 

Qualsiasi aiuto sarà molto apprezzato.

+3

Sulla base di ciò che si è disposti a fare "Voglio testare i controller dell'unità". Vorrei suggerire di aggiungere un altro livello per i tuoi dati. Repository per esempio. Secondo S.O.L.I.D il tuo controller fa troppo personale. – skalinkin

risposta

2

tenta di aggiungere questo:

public interface IEmployeeRepository 
{ 
    Employee GetById(string id); 
} 

public class EmployeeRepository: IEmployeeRepository 
{ 
    public Employee GetById() {//access to OData} 
} 

e poi cambiare il controller di

public JsonResult GetEmployees(string employeeId) 
    { 
     var employee = employeeRepository.GetById(employeeId); 
     return Json(employee, JsonRequestBehavior.AllowGet); 
    } 

Allora si sarà in grado di facilmente Data Access Layer.

+1

Grazie per i tuoi input. In realtà stiamo facendo la stessa cosa che hai suggerito. Ma ho pensato, non ci sarebbe stato alcun supporto inerente a Moq, come se ci prendessimo gioco di DbContext e IDbSet di EF –

Problemi correlati