开发者

Why are my Outbound URLs in NUnit different from the ones in my View?

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.

I noticed that when I ran my HTMLHelper code in NUnit the links appeared like this:

<a href="Page1">1</a>
<a class="selected" href="Page2">2</a>
<a href="Page3">3</a>

But when I see them in the View they look like this:

<a href="/">1</a>
<a class="selected" href="/Page2">2</a>
<a href="/Page3">3</a>

Here's my route 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+" }
 );

Helper code

public static string PageLinks(this HtmlHelper html, int currentPage, 
    int totalPages, Func<int, string> pageUrl)
{
    StringBuilder result = new StringBuilder();
    for (int i = 1; i <= totalPages; i++)
    {
        TagBuilder tag = new TagBuilder("a");
        tag.MergeAt开发者_如何学JAVAtribute("href", pageUrl(i));
        tag.InnerHtml = i.ToString();
        if (i == currentPage)
            tag.AddCssClass("selected");
        result.AppendLine(tag.ToString());                
    }
    return result.ToString();
}

View Code

 <%= Html.PageLinks(2,3,i=>Url.Action("List",new {page=i})) %>

test code

string links = ((HtmlHelper)null).PageLinks(2, 3, i => "Page" + i);
            Assert.AreEqual(@"<a href=""Page1"">1</a>
<a class=""selected"" href=""Page2"">2</a>
<a href=""Page3"">3</a>
", links);

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. 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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜