Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I used Jon Canning's updated version (<a href="https://github.com/JonCanning/MvcIntegrationTestFramework/" rel="nofollow">https://github.com/JonCanning/MvcIntegrationTestFramework/</a>) and it solved my problem very well for controller methods that only accept value types and strings, but did not work for those that accepted classes.</p> <p>It turns out there was an issue with the code for the updated <code>MvcIntegrationTestFramework</code>.</p> <p>I figured out how to fix it, but don't know where else to post the solution, so here it is:</p> <p>A simple sample to show how it works is:</p> <pre><code> [TestMethod] public void Account_LogOn_Post_Succeeds() { string loginUrl = "/Account/LogOn"; appHost.Start(browsingSession =&gt; { var formData = new RouteValueDictionary { { "UserName", "myusername" }, { "Password", "mypassword" }, { "RememberMe", "true"}, { "returnUrl", "/myreturnurl"}, }; RequestResult loginResult = browsingSession.Post(loginUrl, formData); // Add your test assertions here. }); } </code></pre> <p>The call to <code>browsingSession.Post</code> would ultimately cause the <code>NameValueCollectionConversions.ConvertFromRouteValueDictionary(object anonymous)</code> method to be called, and the code for that was:</p> <pre><code>public static class NameValueCollectionConversions { public static NameValueCollection ConvertFromObject(object anonymous) { var nvc = new NameValueCollection(); var dict = new RouteValueDictionary(anonymous); // ** Problem 1 foreach (var kvp in dict) { if (kvp.Value == null) { throw new NullReferenceException(kvp.Key); } if (kvp.Value.GetType().Name.Contains("Anonymous")) { var prefix = kvp.Key + "."; foreach (var innerkvp in new RouteValueDictionary(kvp.Value)) { nvc.Add(prefix + innerkvp.Key, innerkvp.Value.ToString()); } } else { nvc.Add(kvp.Key, kvp.Value.ToString()); // ** Problem2 } } return nvc; } </code></pre> <p>Then there was two problems: </p> <ol> <li>The call to <code>new RouteValueDictionary(anonymous)</code> would cause the "new" <code>RouteValueDictionary</code> to be created, but instead of 4 keys, there are only three, one of which was an array of 4 items.</li> <li>When it hits this line: <code>nvc.Add(kvp.Key, kvp.Value.ToString()</code>, the <code>kvp.Value</code> is an array, and the ToString() gives: <code>"System.Collections.Generic.Dictionary'2+ValueCollection[System.String,System.Object]"</code></li> </ol> <p>The fix (to my specific issue) was to change the code as follows:</p> <pre><code> var dict = anonymous as RouteValueDictionary; // creates it properly if (null == dict) { dict = new RouteValueDictionary(anonymous); } </code></pre> <p>After I made this change, then my model class would properly bind, and all would be well.</p>
    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.
    1. This table or related slice is empty.
    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