Here is my test:
[TestFixture]
public class DisplayingPageLinks
{
[Test]
public void Can_Generate_Links_To_Other_Pages()
{
//Arrange: We're going to extend the Html helper class.
//It doesn't matter if the variable we use is null
HtmlHelper html = null;
PagingInfo pagingInfo = new PagingInfo(){
CurrentPage = 2,
TotalItems = 28,
ItemsPerPage = 10
};
Func<int, String> pageUrl = i => "Page" + 1;
//Act: Here's how it should format the links.
MvcHtmlString result = html.PageLinks(pagingInfo, pageUrl);
//Assert:
result.ToString().ShouldEqual(@"<a href=""Page1"">1</a><a href=""Page2"">2</a><a href=""Page3"">3</a>");
}
}
Here is the PageLinks extension method:
public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo, Func<int,string> pageUrl)
{
StringBuilder result = new StringBuilder();
for (int i = 1; i < pagingInfo.TotalPages; i++)
{
TagBuilder tag = new TagBuilder("a");
tag.MergeAttribute("href", pageUrl(i));
tag.InnerHtml = i.ToString();
if (i == pagingInfo.CurrentPage)
{
tag.AddCssClass("selected");
}
result.Append(tag.ToString());
}
return MvcHtmlString.Create(result.ToString());
}
Finally here is the result for the running test:
SportsStore.UnitTests.DisplayingPageLinks.Can_Generate_Links_To_Other_Pages: Expected string length 63 but was 59. Strings differ at index 24.
Expected: "123" But was: "12"
-----------------------------------^
The error doesn't copy the way it's shown in the GUI - sorry.
Can you give me some suggestions as to why NUnit is saying it receives something I don't expect it to give out.
According to what I'm reading in the PageLinks extension method, it seems that the markup should be formed correctly.
Any suggestions? I'm a TDD newbie and really trying to learn here. :)
Edit:
It seems the culprit was this. My Test was using: Func pageUrl = i => "Page" + 1;
instead of
Func pageUrl = i => "Page" + i;
But now there's another error. :(
It seems something is wrong when calculating the amount of pages in the PagingInfo class:
public class PagingInfo
{
public int TotalItems { get; set; }
public int ItemsPerPage { get; set; }
public int CurrentPage { get; set; }
public int TotalPages
{
get { return (int)Math.Ceiling((decimal)TotalItems / ItemsPerPage); }
}
}
It seems the error stems on the fact that this is returning 2 pages, instead of 3.
Is there something wrong in this calculation?