tags:

views:

35

answers:

1

Hi,

I do not understand how the following unit test instruction

string links = ((HtmlHelper)null).PageLinks(2, 3, i => "Page" + i);

maps to actual code as follows:

<%= Html.PageLinks((int)ViewData["CurrentPage"], (int)ViewData["TotalPages"], x=> Url.Action("List", new { page = x})) %>

The part that confuses me is that how...

i => "Page" + i

is testing the following:

x=> Url.Action("List", new { page = x})
+1  A: 

You should post the PageLinks code for beter Understanding, but i think i know what id does anyway (is this from steve sanderson MVC book?)

The third parameter from the PageLinks its a function that receive a int parameter (pageNumber) and return a string, this function gets called for each link the helper is going to generate, to obtain the URL each link points to. In your code you are defining this function using Lambda expressions (http://msdn.microsoft.com/en-us/library/bb397687.aspx).

For example, when you call your helper on the test:

(string links = ((HtmlHelper)null).PageLinks(2, 3, i => "Page" + i);) 

you should receive something like this:

<a href='Page1'></a> <a href='Page2'></a> <a href='Page3'></a>

notice how the href change for each link, thats what you have to check on the result to see if the helper is working.

x=> Url.Action("List", new { page = x})

works the same way, your helper call Url.Action for each link it needs to generate.

David Martinez