This is a migrated thread and some comments may be shown as answers.

[Solved] Unit tests for Grid

2 Answers 42 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Vangel
Top achievements
Rank 1
Vangel asked on 21 Sep 2012, 12:18 PM
Hi Guys,

I'm trying to test an extension method which create a generic grid:

public static void GetEntityGrid<T>(HtmlHelper helper) where T : class
{           
      helper.Telerik().Grid<T>()
                .Name("Grid_{0}".FormatWith(Guid.NewGuid()))
                .HtmlAttributes(htmlAttributes)
                .ToolBar(commands => SetToolbar(helper, commands, metadata, toolBarActions, entityFormattedName, routeValues, controller))
                .Columns(columns => SetColumns(helper, columns, metadata, entityDisplayColumn, controller, itemActions, columnWidths))
                .DataBinding(binding => binding.Ajax().Select("GetGridData", controller, routeValues))
                .Sortable(sorting => SetSorting(sorting, metadata))
                .Pageable(x => x.PageSize(GuiSettings.Global.GetSetting(GlobalSettingType.DefaultPageSize).ValueAs<int>()))
                .ClientEvents(events => SetClientEvents(events, clientEvents))
                .Scrollable(scrolling => scrolling.Enabled(autoScroll).Height("auto"))
                .Render();
}

Here is my test method:

[TestMethod]
public void EntityGridTest()
{
    MockToolsMvc.SetFakeHttpContext();
    HtmlHelper htmlHelper = MockToolsMvc.FakeHtmlHelper();
    htmlHelper.EntityGrid<TestingClass>();
 }

But I receive following error: Object reference not set to an instance of an object.

Actually, the problem is that the grid can't find his resource file. Take a look at the StackTrace:
   at Telerik.Web.Mvc.Infrastructure.Implementation.VirtualPathProviderWrapper.FileExists(String virtualPath)
   at Telerik.Web.Mvc.Infrastructure.Implementation.LocalizationService.CreateResource(String resourceName, CultureInfo culture, String resourceLocation)
   at Telerik.Web.Mvc.Infrastructure.Implementation.LocalizationService.DetectResource(String resourceLocation, String resourceName, CultureInfo culture)
   at Telerik.Web.Mvc.Infrastructure.Implementation.LocalizationService..ctor(String resourceName, CultureInfo culture)
   at Telerik.Web.Mvc.Infrastructure.Implementation.LocalizationServiceFactory.Create(String resourceName, CultureInfo culture)
   at Telerik.Web.Mvc.UI.ViewComponentFactory.<Grid>b__2[T]()
   at Telerik.Web.Mvc.UI.ViewComponentFactory.Register[TViewComponent](Func`1 factory)
   at Telerik.Web.Mvc.UI.ViewComponentFactory.Grid[T]()
    
Is it possible to disable localization in testing purpose? Or can I mock and inject this localization service? Or can I fake this resource file some how?

Best regards,
Vangel

2 Answers, 1 is accepted

Sort by
0
Accepted
Atanas Korchev
Telerik team
answered on 25 Sep 2012, 05:45 AM
Hi,

 The localization service is determined at runtime using dependency injection:

public virtual GridBuilder<T> Grid<T>() where T : class
{
    return GridBuilder<T>.Create(Register(() => new Grid<T>(ViewContext,
                ClientSideObjectWriterFactory,
                DI.Current.Resolve<IUrlGenerator>(),
                DI.Current.Resolve<ILocalizationServiceFactory>().Create("GridLocalization", CultureInfo.CurrentUICulture),
                DI.Current.Resolve<IGridHtmlBuilderFactory>()
            )
        )
    );
}

You can register a custom ILocalizationServiceFactory with our DI container before the tests are run. The Create method of the localization service factory should return a custom ILocalizationService implementation which the grid will use. Here is a quick code snippet:

public class MyLocalizationServiceFactory : ILocalizationServiceFactory
{
    public ILocalizationService Create(string resourceName, CultureInfo culture)
    {
       return new MyLocalizationService();
    }
}
 
public class MyLocalizationService : ILocalizationService
{
     public string One(string key)
     {
      return String.Empty;
     }
 
     public IDictionary<string, string> All()
     {
      return new Dictionary<string, string>();
     }
 
     public IsDefault
     {
      get
      {
       return true;
      }
     }
}
 
public void EntityGridTest()
{
  MockToolsMvc.SetFakeHttpContext();
 
  DI.Current.Register<ILocalizationServiceFactory>(() => new MyLocalizationServiceFactory 
());
  HtmlHelper htmlHelper = MockToolsMvc.FakeHtmlHelper();
  htmlHelper.EntityGrid<TestingClass>();
}

Kind regards,
Atanas Korchev
the Telerik team
Check out the successor of Telerik MVC Extensions - Kendo UI for ASP.NET MVC - and deem it for new ASP.NET MVC development.
0
Vangel
Top achievements
Rank 1
answered on 25 Sep 2012, 09:46 AM
Hi Atanas,

This works perfect. Thanks for the explanation and example code.

Best regards,
Vangel
Tags
Grid
Asked by
Vangel
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Vangel
Top achievements
Rank 1
Share this question
or