My extension method is not registered

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

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

Now I am having problems with the test that the book requests from us.

[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 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 an HtmlHelper variable. The PageLinks () extension method seems to be incorrectly registered.

Where can I check this out? I understand that this question can be a little vague, but any help would be great.

EDIT:

Obviously, this is where I registered the extension method. Although it does not seem to expand anything. At least intellisnse doesn't show it when I inject it into the code above.

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());         
        }
    }

, - , Visual Studio, ?

2: Woops! :

1 'System.Web.Mvc.HtmlHelper' "PageLinks" "PageLinks" "System.Web.Mvc.HtmlHelper" ( ?) C:\Users\Sergio\documents\visual 2010\Projects\SportsStore\SportsStore.UnitTests\DisplayingPageLinks.cs 35 41 SportsStore.UnitTests

+3
5

, , . , (, , ). , this , . , .

+8

web.config :

<system.web>
 <pages>
  <namespaces>
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    <add namespace="System.Web.Routing" />
    <add namespace="System.Linq"/>
    <add namespace="System.Collections.Generic"/>
    <add namespace="YourNameSpace.HtmlHelpers"/>
  </namespaces>
</pages>

+3

MvcHtmlString result = PagingHelpers.PageLinks(myHelper, pagingInfo, pageUrlDelegate);

MvcHtmlString result = myHelper.PageLinks(pagingInfo, pageUrlDelegate);
0

List.cshtml

@model SportsStore.WebUI.Models.ProductsListViewModel
0

this HtmlHelper html, PageLinks PagingHelpers HtmlHelper PagingHelpers MvcHtmlString result = PagingHelpers.PageLinks(pagingInfo, pageUrl);

HtmlHelper .

0

Source: https://habr.com/ru/post/1758562/


All Articles