'Unit test base Controller OnActionExecutionAsync and OnActionExecuted methods

I have a base controller which I set some common controller properties for in the OnActionExecutionAsync method, and then set some common viewmodel properties in the OnActionExecuted method this is working fine, but I now need to write some unit tests for it. no tdd for me..

the code I have is :

public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
    //set some controller properties
    await base.OnActionExecutionAsync(context, next);
}

public override void OnActionExecuted(ActionExecutedContext filterContext)
{
    //set some other viewmodel properties
    base.OnActionExecuted(filterContext);
}

the problem I have with the OnActionExecutionAsync tests, other than it being a right pain to mock the context, is that when the method completes, it automatically calls the OnActionExecuted method, and falls over as the ActionExecutedContext is null. i am assuming i need to call await base.OnActionExecutionAsync(context, next);, rather than simply await next();? to continue with the pipeline, although having checked the Controller code in github, it seems the base implementation only calls OnActionExecuted when the result form the ActionExecutingContext is null aspnet core controller on GitHub any idea how I can get my test to work without it jumping off to OnActionExecuted?



Solution 1:[1]

You can pass in a lightweight delegate as the "next" item

var httpContext = new DefaultHttpContext();

var actionContext = new ActionContext(_httpContext, new RouteData(), new ActionDescriptor(), new ModelStateDictionary());

var actionExecutingContext = new ActionExecutingContext(actionContext, new List<IFilterMetadata>(), new Dictionary<string, object>(), controller: null);

var executedContext = new ActionExecutedContext(actionContext, new List<IFilterMetadata>(), null);

await actionFilter.OnActionExecutionAsync(actionExecutingContext, () => Task.FromResult(executedContext));

Solution 2:[2]

//try this:

public class reverseArray {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    int arr[] = new int[] { 1,3,4,2,5,6 };

    System.out.print("Original array: ");

    for (int i = 0; i < arr.length; i++) {
        System.out.print(arr[i] + " ");

    }

    // method #1: by using extra array

    System.out.println();
    System.out.print("Reversed array method 1: ");

    int j = 0;
    int revArr[] = new int[] { arr.length };

    for (int i = arr.length - 1; i >= 0; i--) {

        revArr[j] = arr[i];
        System.out.print(revArr[j] + " ");

    }

    // method #2: without using extra array

    for (int i = 0; i < arr.length / 2; i++) {
        int temp = arr[i];
        arr[i] = arr[arr.length - i - 1];
        arr[arr.length - i - 1] = temp;
    }

    System.out.println();
    System.out.print("Reversed array method 2: ");
    for (int i = 0; i < arr.length; i++) {
        System.out.print(arr[i] + " ");
    }
}

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Matthew Steeples
Solution 2 Sandesha