2013-06-19 14 views
6

Kontrolörlerimiz için bazı birim testleri yazma aşamasındayım. Aşağıdaki basit denetleyiciye sahibiz.Ünite Test Kontrolörü Kendo UI ile MVC4'te Eylemler

[Test] 
public void Create() 
{ 
     // Arrange 
     clientController.ModelState.Clear(); 

     // Act 
     JsonResult json = clientController.Create(this.clientDto, this.dataSourceRequest) as JsonResult; 

     // Assert 
     Assert.IsNotNull(json); 

} 

ve kontrol bağlama aşağıdaki kod ile sahte edilir: durum zaman Create denetleyicisi eylem girişimi ortaya

public class FakeControllerContext : ControllerContext 
    { 
     HttpContextBase context = new FakeHttpContext(); 

     public override HttpContextBase HttpContext 
     { 
      get 
      { 
       return context; 
      } 
      set 
      { 
       context = value; 
      } 
     } 

    } 

    public class FakeHttpContext : HttpContextBase 
    { 
     public HttpRequestBase request = new FakeHttpRequest(); 
     public HttpResponseBase response = new FakeHttpResponse(); 

     public override HttpRequestBase Request 
     { 
      get { return request; } 
     } 

     public override HttpResponseBase Response 
     { 
      get { return response; } 
     } 
    } 

    public class FakeHttpRequest : HttpRequestBase 
    { 

    } 

    public class FakeHttpResponse : HttpResponseBase 
    { 

    } 


} 

aşağıdaki gibi

public class ClientController : Controller 
{ 

    [HttpPost] 
    public ActionResult Create(Client client, [DataSourceRequest] DataSourceRequest request) 
    { 
     if (ModelState.IsValid) 
     { 
      clientRepo.InsertClient(client); 
     } 

     return Json(new[] {client}.ToDataSourceResult(request, ModelState)); 
    } 
} 

bu birim test ToDataSourceResult yöntemini çağırmak için.

System.EntryPointNotFoundException : Entry point was not found. 

ayıklama (ve standart bir bağlamında çalışır zaman) ModelState iç sözlük birimi test boş olduğunu gösterir. ModelState, ToDataSourceResult yönteminden kaldırılırsa, sınama başarılı bir şekilde geçer. Herhangi bir yardım çok takdir edilir.

cevap

5

JustDecompile yılında hızlı bir zirve Kendo.Web.Mvc.dllSystem.Web.Mvc sürüm 3.0 karşı inşa edildiğini ortaya koymaktadır. Test projeniz muhtemelen daha yeni olan ASP.NET MVC (4.0) sürümüne başvurur ve bu nedenle System.Web.Mvc üyelerine yapılan tüm çağrılar System.EntryPointNotFoundException sonucunu verir, çünkü bu üyeler çözülemez. Özel durumunuzda, KendoUI MVC uzatma metodu ToDataSourceResult()'a yapılan çağrı ve daha sonra ModelState.IsValid numaralı telefona yapılan çağrı suçluydu.

Uygulamanızın hatasız çalışması nedeni, projenizin varsayılan olarak Visual Studio ASP.NET MVC proje şablonunun bir parçası olarak redirect assembly bindings olarak yapılandırılmasıdır, böylece çalışma zamanı ASP'nin en yeni sürümünü hedefler. NET MVC toplandı. Bunu App.config dosyasında aynı çalışma zamanı bağlama bilgiler ekleyerek sınama proje düzeltebilirsiniz:

<configuration> 
    <runtime> 
     <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
      <dependentAssembly> 
       <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> 
       <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.0.0.0" /> 
      </dependentAssembly> 
     </assemblyBinding> 
    </runtime> 
</configuration> 

Bunu size yardımcı olmuştur.

+0

Çok teşekkürler ... bu sonuca kendim gelmezdim. – aponzani

+0

Bu cevap günümü kurtardı! – vcRobe