Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Unit Test a custom ModelBinder using Moq?
    primarykey
    data
    text
    <p>I'm having some difficulty writing some Unit Tests to test a custom ModelBinder that I created. The ModelBinder I'm trying to Unit Test is the JsonDictionaryModelBinder that I posted <a href="https://stackoverflow.com/questions/1077481/how-do-i-pass-a-dictionary-as-a-parameter-to-an-actionresult-method-from-jquery-a/1080721#1080721">here</a>.</p> <p>The problem I'm having is getting the Mocking all setup using Moq. I keep getting Null Exceptions due to the HttpContextBase not being Mocked correctly. I think.</p> <p>Could someone help me figure out what I'm not doing correclty?</p> <p>Here's a sample of the Unit Test I'm trying to write that doesn't work:</p> <pre><code>[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&lt;Controller&gt;().Object); Predicate&lt;string&gt; predicate = propertyName =&gt; (propertyName == "jsonValues"); ModelBindingContext bindingContext = new ModelBindingContext() { Model = null, ModelType = typeof(JsonDictionary), ModelState = new ModelStateDictionary(), PropertyFilter = predicate, ValueProvider = new Dictionary&lt;string, ValueProviderResult&gt;() { { "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."); } </code></pre> <p>Here's the "FakeHttpContext" method used above:</p> <pre><code>public static class MockHelper { public static HttpContextBase FakeHttpContext(HttpVerbs verbs, NameValueCollection nameValueCollection) { var httpContext = new Mock&lt;HttpContextBase&gt;(); var request = new Mock&lt;HttpRequestBase&gt;(); request.Setup(c =&gt; c.Form).Returns(nameValueCollection); request.Setup(c =&gt; c.QueryString).Returns(nameValueCollection); var response = new Mock&lt;HttpResponseBase&gt;(); var session = new Mock&lt;HttpSessionStateBase&gt;(); var server = new Mock&lt;HttpServerUtilityBase&gt;(); httpContext.Setup(c =&gt; c.Request).Returns(request.Object); var u = verbs.ToString().ToUpper(); httpContext.Setup(c =&gt; c.Request.RequestType).Returns( verbs.ToString().ToUpper() ); httpContext.Setup(c =&gt; c.Response).Returns(response.Object); httpContext.Setup(c =&gt; c.Server).Returns(server.Object); httpContext.Setup(c =&gt; c.User.Identity.Name).Returns("testclient"); return httpContext.Object; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload