Testing custom ModelBinder against HTTP context in ASP.NET MVC (1.0)
I'm trying to unit test a custom model binder - specifically, I want to see how it responds to various (possibly conflicting) values being submitted in the Request.Form and Request.QueryString coll开发者_如何学编程ections - i.e. if I submit one value in the form and another in the querystring (yeah, yeah, I know, this is evil, but I want test coverage in case it happens) I can validate exactly which one will be bound to the model.
In order to do this, I'd like to mock/fake the HTTP context, and then invoke the model binder and see what is actually returned. I've seen several posts about testing ModelBinders, but all of them use a custom ValueProvider, whereas I actually want to test the way MVC is interacting with Form/Request collections.
Any ideas how I can mock these collections and then cause my model binder to use the 'default' ValueProvider based on this mocked HTTP context in my units tests? This is on ASP.NET MVC 1.0. Thanks.
Nailed it - the solution is to mock the ControllerContext, and then construct a new System.Web.Mvc.ValueProviderDictionary and pass your mocked controller context into the constructor, as follows:
[Test]
public void WorkFolder_Id_Is_Parsed_From_QueryString() {
var fakeControllerContext = GetControllerContext(null, "folder=10");
var bindingContext = new ModelBindingContext() {
ValueProvider = new System.Web.Mvc.ValueProviderDictionary(fakeControllerContext),
ModelName = "menu",
FallbackToEmptyPrefix = true
};
var binder = new RenewalMenuPostModelBinder();
var model = binder.BindModel(fakeControllerContext, bindingContext) as RenewalMenuPostModel;
Assert.That(model is RenewalMenuPostModel);
Assert.That(model.WorkFolderId.HasValue);
Assert.That(model.WorkFolderId.Value == 10);
}
private static ControllerContext GetControllerContext(NameValueCollection form, string queryString) {
Mock<HttpRequestBase> mockRequest = new Mock<HttpRequestBase>();
mockRequest.Expect(r => r.Form).Returns(form);
var queryStringCollection = HttpUtility.ParseQueryString(queryString);
mockRequest.Expect(r => r.QueryString).Returns(queryStringCollection);
Mock<HttpContextBase> mockHttpContext = new Mock<HttpContextBase>();
mockHttpContext.Expect(c => c.Request).Returns(mockRequest.Object);
return new ControllerContext(mockHttpContext.Object, new RouteData(), new Mock<ControllerBase>().Object);
}
精彩评论