AspNetCoreOData
AspNetCoreOData copied to clipboard
Can't unit test classes derived from EnableQueryAttribute due to RequestQueryData
I have classes derived from EnableQueryAttribute that can no longer be unit tested due to the introduction of the RequestQueryData object in commit c7bd4d750bb5682a362ee269087fdf15f1481931. The code in question is
private ODataQueryOptions CreateAndValidateQueryOptions(HttpRequest request, ODataQueryContext queryContext)
{
RequestQueryData requestQueryData = request.HttpContext.Items[nameof(RequestQueryData)] as RequestQueryData;
if (requestQueryData.QueryValidationRunBeforeActionExecution)
{
return requestQueryData.ProcessedQueryOptions;
}
Note that the RequestQueryData object is expected to be there, and no null check is performed. In a unit test of my attribute, I'm using a constructed HttpContext and I would insert it if I could, but it's a private class. Please consider making the class public or using an interface so that it can be mocked.
Thanks.
Please note this is still an issue in OData V8. If I have my own class that inherits from EnableQueryAttribute and I override OnActionExecuted, I can't unit test my code in isolation because there no RequestQueryData object in HttpContext.Items.
This should only need a very simple change to fix:
private ODataQueryOptions CreateAndValidateQueryOptions(HttpRequest request, ODataQueryContext queryContext)
{
RequestQueryData requestQueryData = request.HttpContext.Items[nameof(RequestQueryData)] as RequestQueryData;
if (requestQueryData?.QueryValidationRunBeforeActionExecution ?? false)
{
return requestQueryData.ProcessedQueryOptions;
}
This appears to be low-hanging fruit, so is there any way someone can get to this? Thanks.