Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy are my Outbound URLs in NUnit different from the ones in my View?
    text
    copied!<p>I'm reading Pro ASP.NET MVC Framework book. In an example on 106 - 111 the author walks through creating an NUnit test for a HTMLHelper class.</p> <p>I noticed that when I ran my HTMLHelper code in NUnit the links appeared like this:</p> <pre><code>&lt;a href="Page1"&gt;1&lt;/a&gt; &lt;a class="selected" href="Page2"&gt;2&lt;/a&gt; &lt;a href="Page3"&gt;3&lt;/a&gt; </code></pre> <p>But when I see them in the View they look like this:</p> <pre><code>&lt;a href="/"&gt;1&lt;/a&gt; &lt;a class="selected" href="/Page2"&gt;2&lt;/a&gt; &lt;a href="/Page3"&gt;3&lt;/a&gt; </code></pre> <p><strong>Here's my route code:</strong></p> <pre><code>routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( null, "", new { controller = "Products", action = "List", page = 1 } ); routes.MapRoute( null, "Page{page}", new {controller = "Products",action="List"}, new {page=@"\d+" } ); </code></pre> <p><strong>Helper code</strong></p> <pre><code>public static string PageLinks(this HtmlHelper html, int currentPage, int totalPages, Func&lt;int, string&gt; pageUrl) { StringBuilder result = new StringBuilder(); for (int i = 1; i &lt;= totalPages; i++) { TagBuilder tag = new TagBuilder("a"); tag.MergeAttribute("href", pageUrl(i)); tag.InnerHtml = i.ToString(); if (i == currentPage) tag.AddCssClass("selected"); result.AppendLine(tag.ToString()); } return result.ToString(); } </code></pre> <p><strong>View Code</strong></p> <pre><code> &lt;%= Html.PageLinks(2,3,i=&gt;Url.Action("List",new {page=i})) %&gt; </code></pre> <p><strong>test code</strong></p> <pre><code>string links = ((HtmlHelper)null).PageLinks(2, 3, i =&gt; "Page" + i); Assert.AreEqual(@"&lt;a href=""Page1""&gt;1&lt;/a&gt; &lt;a class=""selected"" href=""Page2""&gt;2&lt;/a&gt; &lt;a href=""Page3""&gt;3&lt;/a&gt; ", links); </code></pre> <p>I understand that the urls are created based on information in my routing table so I'm not sure why the links would be different. <strong>Does NUnit not have the same perspective that the view does and thus creates different links? How can I get NUnit to generate the same exact links my View does?</strong></p>
 

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