13
我在編寫一些單元測試來測試我創建的自定義ModelBinder時遇到了一些困難。我想單元測試的ModelBinder是我發佈的JsonDictionaryModelBinder here。如何使用Moq測試自定義ModelBinder?
我遇到的問題是使用Moq模擬所有設置。由於HttpContextBase未被正確模擬,我一直收到空異常。我認爲。
有人可以幫我弄清楚我沒有做的事嗎?
這裏的單元測試我想寫的樣本不工作:
[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.");
}
下面是上面所用的「FakeHttpContext」的方法:
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;
}
}
真棒的感謝! – 2009-07-04 12:22:16