2009-07-03 23 views
13

Oluşturduğum özel bir ModelBinder'ı test etmek için bazı Birim Testleri yazmakta zorluk çekiyorum. Birim Testi denemeye çalıştığım ModelBinder, here gönderdiğim JsonDictionaryModelBinder'tır.Nasıl Birim Birim Özel ModelBinder'i kullanarak?

Yapmakta olduğum sorun, tüm ayarları Moq. HttpContextBase doğru alay edilmeyen nedeniyle NULL özel durumlar almaya devam ediyorum. Bence.

Birisi düzeltmeyi yapmadığım şeyi anlamaya yardımcı olabilir mi?

İşte
[TestMethod()] 
public void BindModelTest() 
{ 
    JsonDictionaryModelBinder target = new JsonDictionaryModelBinder(); 

    NameValueCollection nameValueCollection = new NameValueCollection() { 
     {"First", "1"}, 
     {"Second", "2"}, 
     {"Name", "Chris"}, 
     {"jsonValues", "{id: 200, name: 'Chris'}"} 
    }; 

    HttpContextBase httpContext = MockHelper.FakeHttpContext(HttpVerbs.Post, nameValueCollection); 

    ControllerContext controllerContext = 
     new ControllerContext(new RequestContext(httpContext, new RouteData()), new Mock<Controller>().Object); 


    Predicate<string> predicate = propertyName => (propertyName == "jsonValues"); 
    ModelBindingContext bindingContext = new ModelBindingContext() 
    { 
     Model = null, 
     ModelType = typeof(JsonDictionary), 
     ModelState = new ModelStateDictionary(), 
     PropertyFilter = predicate, 
     ValueProvider = new Dictionary<string, ValueProviderResult>() { { "foo", null } } 
    }; 

    //object expected = null; // TODO: Initialize to an appropriate value 
    var actual = target.BindModel(controllerContext, bindingContext) as JsonDictionary; 

    Assert.IsNotNull(actual); 

    Assert.AreEqual("Chris", actual["name"]); 
    //Assert.AreEqual(expected, actual); 
    Assert.Inconclusive("Verify the correctness of this test method."); 
} 

yukarıda kullanılan "FakeHttpContext" yöntemidir:

public static class MockHelper 
{ 
    public static HttpContextBase FakeHttpContext(HttpVerbs verbs, NameValueCollection nameValueCollection) 
    { 
     var httpContext = new Mock<HttpContextBase>(); 

     var request = new Mock<HttpRequestBase>(); 
     request.Setup(c => c.Form).Returns(nameValueCollection); 
     request.Setup(c => c.QueryString).Returns(nameValueCollection); 

     var response = new Mock<HttpResponseBase>(); 
     var session = new Mock<HttpSessionStateBase>(); 
     var server = new Mock<HttpServerUtilityBase>(); 
     httpContext.Setup(c => c.Request).Returns(request.Object); 

     var u = verbs.ToString().ToUpper(); 
     httpContext.Setup(c => c.Request.RequestType).Returns(
      verbs.ToString().ToUpper() 
     ); 

     httpContext.Setup(c => c.Response).Returns(response.Object); 
     httpContext.Setup(c => c.Server).Returns(server.Object); 
     httpContext.Setup(c => c.User.Identity.Name).Returns("testclient"); 
     return httpContext.Object; 
    } 
} 

cevap

7

suçlu şudur İşte

ben yazmaya çalışıyorum Birim Testi örnek bu işe yaramazsa var çizgi:

httpContext.Setup(c => c.Request.RequestType).Returns(
       verbs.ToString().ToUpper() 
      ); 

Bu ayrıntılı nesne üzerinde ikinci Setup bir, teknik olarak d Nesne hiyerarşisinde "geçmiş" olmanıza rağmen orijinal Setup'u siliyor. Bunun, Moq'da bir hata mı yoksa istenen davranış mı olduğu konusunda emin değilim, bunu daha önce de yaptım ve kontrol etmek için etrafta dolanmadım.

Yukarıdaki satırı, isteğinizi yukarıda belirlediğiniz yere taşıyarak ve httpContext uygulamasına gitmek yerine doğrudan ayarlayarak sorunu çözebilirsiniz. Yani,

request.Setup(c => c.RequestType).Returns(verbs.ToString().ToUpper()); 

Ben de beyan "var u" kullanılmadığından fark;)

+0

Müthiş teşekkür! –