views:

37

answers:

1

I'm following Pro ASP.Net MVC2 book and literally 90% of everything is new to me. I feel like a kid in a candy store! :)

Unit testing, dependancy injection, and other things are really new and very foreign to the typical CRUD applications I create.

Now I'm having trouble with a test the book asks us to design.

[Test]
        public void Can_Generate_Links_To_Other_Pages()
        {
            // Arrange: We're going to extend the HtmlHelper class.
            // It doesn't matter if the variable we use is null.
            HtmlHelper html = null;

            // Arrange: The helper should take a PagingInfo instance (that's
            // a class we haven't yet defined) and a lambda to specify the URLs
            PagingInfo pagingInfo = new PagingInfo
            {
                CurrentPage = 2,
                TotalItems = 28,
                ItemsPerPage = 10
            };

            Func<int, string> pageUrl = i => "Page" + i;

            // Act
            MvcHtmlString result = html.PageLinks(pagingInfo, pageUrl);

            // Assert: Here's how it should format the links
            result.ToString().ShouldEqual(@"<a href=""Page1"">1</a>
                                            <a class=""selected"" href=""Page2"">2</a>
                                            <a href=""Page3"">3</a>");
        }

My html variable is a HtmlHelper variable. It seems that the extension method PageLinks() isn't correctly registered.

Where would I check for this? I realize this question might be a bit vague, but any help will be wonderful.

EDIT:

Apparantly this is where I registered the extension method. Although it doesn't seem to extend anything. At least intellisnse doesn't show it when I type it in the above code.

public static class PagingHelpers
    {
        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"); // Construct an <a> tag
                tag.MergeAttribute("href", pageUrl(i));
                tag.InnerHtml = i.ToString();
                if (i == pagingInfo.CurrentPage)
                    tag.AddCssClass("selected");
                result.AppendLine(tag.ToString());
            }

            return MvcHtmlString.Create(result.ToString());         
        }
    }

Also, can someone tell me how to set up Visual Studio so it just copies plain text without it's ridiculous indentation?

EDIT 2: Woops! Forgot to type in the error:

Error 1 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'PageLinks' and no extension method 'PageLinks' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?) C:\Users\Sergio\documents\visual studio 2010\Projects\SportsStore\SportsStore.UnitTests\DisplayingPageLinks.cs 35 41 SportsStore.UnitTests

+4  A: 

To use an extension method, you need to include the namespace in which the extension method class resides. You also need to ensure the extension method class is static and accessible to consuming code (e.g. can't be internal if it's in another assembly). Finally, be sure not to forget the this keyword on the type you're extending. If all those things are in place, you shouldn't see a problem.

Rex M
Also, if you're writing the extension method yourself, don't forget the `this` parameter keyword.
chaiguy
You're right, I had to add the using namespace so it would register.
Sergio Tapia
@Sergio feel free to accept the answer if it solves your problem
Rex M