Unit Test with ViewModels that have attributes of type JqGridColumnElementAttribute

Feb 11, 2012 at 12:37 PM
Edited Feb 11, 2012 at 12:43 PM

Hi!

I have a serious problems with unit test when viewmodels have a JqGridColumnElementAttribute, for example JqGridColumnSearchableAttribute. It's because exists a directly dependency with HttpContext.Current when is necessary use a internal property called DataUrl. 

I use NUnit, Rhino Mocks and MvcContrib for help me with mock unit tests, but I can handle ControllerContext only. When I make a FakeHttpContext, with SimpleRequestWorker and so on, Routes.GetVirtualPath and Routes.GetVirtualPathArea not works properly.

I guess you can make this more easy, possibiliting the mock of this dependency. And if you have a suggestion to me, how I can apply a workaround for this, I really appreciate.

Thanks for your attention and wonderful lib.

 

Regards,

Anderson Correia.

Feb 11, 2012 at 1:17 PM

It can be helpful: http://aspnet.codeplex.com/workitem/750

Feb 11, 2012 at 1:22 PM

Ah! StackTrace below:

System.InvalidOperationException : The DataUrl could not be resolved. at Lib.Web.Mvc.JQuery.JqGrid.DataAnnotations.JqGridColumnElementAttribute.get_DataUrl() at Lib.Web.Mvc.JQuery.JqGrid.DataAnnotations.JqGridColumnSearchableAttribute.InternalOnMetadataCreated(ModelMetadata metadata) at Lib.Web.Mvc.JQuery.JqGrid.DataAnnotations.JqGridColumnElementAttribute.OnMetadataCreated(ModelMetadata metadata) at System.Web.Mvc.AssociatedMetadataProvider.ApplyMetadataAwareAttributes(IEnumerable`1 attributes, ModelMetadata result) at System.Web.Mvc.AssociatedMetadataProvider.GetMetadataForProperty(Func`1 modelAccessor, Type containerType, PropertyDescriptor propertyDescriptor) at System.Web.Mvc.AssociatedMetadataProvider.<GetMetadataForPropertiesImpl>d__2.MoveNext() at System.Linq.Buffer`1..ctor(IEnumerable`1 source) at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__0.MoveNext() at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext() at Lib.Web.Mvc.JQuery.JqGrid.JqGridRecord`1.GetValuesAsList() at Lib.Web.Mvc.JQuery.JqGrid.JqGridRecord`1..ctor(String id, TModel value) Controllers\BaseController.cs(44,0): at TheConsumerApplication.Web.Controllers.BaseController.<JqGridJson>b__2[T](T row) at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext() at System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection) at System.Collections.Generic.List`1.AddRange(IEnumerable`1 collection) Controllers\BaseController.cs(53,0): at TheConsumerApplication.Web.Controllers.BaseController.JqGridJson[T](IEnumerable`1 data, Int32 pageIndex, Int32 pageSize, Int32 totalRecords) Controllers\BaseController.cs(38,0): atTheConsumerApplication.Web.Controllers.BaseController.JqGridJson[T](DataResult`1 result) Controllers\ProductFamilyController.cs(42,0): at TheConsumerApplication.Web.Controllers.ProductFamilyController.ListFamilies(JqGridRequest request) ProductFamilyControllerTest.cs(363,0): at TheConsumerApplication.Web.Tests.ProductFamilyControllerTest.Must_Return_JqGridResult()

Coordinator
Feb 11, 2012 at 2:45 PM

Thank you for all the details, I will look into this.

Feb 17, 2012 at 11:04 AM

This approach in the lib may help:

var httpContext = HttpContext.Current;

if (httpContext == null) {
 
var request = new HttpRequest("/", "http://example.com", "");
 
var response = new HttpResponse(new StringWriter());
  httpContext
= new HttpContext(request, response);
}

var httpContextBase = new HttpContextWrapper(httpContext);
var routeData = new RouteData();
var requestContext = new RequestContext(httpContextBase, routeData);

return new UrlHelper(requestContext);

Font: http://stackoverflow.com/questions/2031995/call-urlhelper-in-models-in-asp-net-mvc

Coordinator
Feb 23, 2012 at 8:31 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.