@@ -318,6 +318,43 @@ private static IEnumerable<ControllerActionDescriptor> CreateDescriptors(string
318318 return descriptors ;
319319 }
320320
321+ [ Fact ]
322+ public void Derived_Class_Can_Unit_Test_When_OnActionExecuted_Is_Overridden ( )
323+ {
324+ // https://github.com/OData/AspNetCoreOData/issues/239: unit tests for derived classes fail because RequestQueryOptions missing from HttpContext.Items
325+ IEdmModel model = new CustomersModelWithInheritance ( ) . Model ;
326+
327+ var request = RequestFactory . Create ( "Get" , "http://localhost/Customers?$filter=Id+eq+1" , opt => opt . AddRouteComponents ( "odata" , model , services => { } ) . Filter ( ) ) ;
328+
329+ ODataUriParser parser = new ODataUriParser ( model , new Uri ( "Customers" , UriKind . Relative ) ) ;
330+ HttpContext httpContext = request . HttpContext ;
331+
332+ Assert . NotNull ( httpContext . RequestServices ) ;
333+
334+ httpContext . Request . Method = "Get" ;
335+ request . Configure ( "odata" , model , parser . ParsePath ( ) ) ;
336+ ActionDescriptor actionDescriptor = CreateDescriptors ( "CustomersController" , typeof ( CustomersController ) )
337+ . First ( descriptor => descriptor . ActionName . StartsWith ( "Get" , StringComparison . OrdinalIgnoreCase ) ) ;
338+ ActionContext actionContext = new ActionContext ( httpContext , new RouteData ( ) , actionDescriptor ) ;
339+
340+ ActionExecutedContext context = new ActionExecutedContext ( actionContext , new List < IFilterMetadata > ( ) , new CustomersController ( ) ) ;
341+ context . Result = new ObjectResult ( new List < Customer > ( ) { new Customer { Id = 1 , Name = "John Doe" } } ) { StatusCode = 200 } ;
342+
343+ MyEnableQueryAttribute attribute = new MyEnableQueryAttribute ( ) ;
344+
345+ // Act and Assert
346+ ExceptionAssert . DoesNotThrow ( ( ) => attribute . OnActionExecuted ( context ) ) ;
347+
348+ }
349+
350+ private class MyEnableQueryAttribute : EnableQueryAttribute
351+ {
352+ public override void OnActionExecuted ( ActionExecutedContext actionExecutedContext )
353+ {
354+ base . OnActionExecuted ( actionExecutedContext ) ;
355+ }
356+ }
357+
321358#if NETCORE // Following functionality is only supported in NetCore.
322359 [ Fact ]
323360 public void OnActionExecuting_Throws_Null_Context ( )
0 commit comments