How to mock ActionExecutingContext with Moq?
Asked Answered
M

2

30

I am trying to test the following filter:

using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.Filters;

namespace Hello
{
    public class ValidationFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (!filterContext.ModelState.IsValid) {
                filterContext.Result = new BadRequestObjectResult(filterContext.ModelState);
            }
        }
    }
}

I am trying to mock the ActionFilterAttribute using Moq.

I am using Mvc 6.0.0-rc1

My first try:

var context = new ActionExecutingContext(
                    new ActionContext(Mock.Of<HttpContext>(), new RouteData(), new ActionDescriptor()),
                    new IFilterMetadata[] { filter, },
                    new Dictionary<string, object>(),
                    controller: new object());

But I could not override the ModelState. It looks to be readonly: https://github.com/aspnet/Mvc/blob/6.0.0-rc1/src/Microsoft.AspNet.Mvc.Abstractions/ActionContext.cs#L25

Then I tried:

var contextMock = new Mock<ActionExecutingContext>(
                    new ActionContext(Mock.Of<HttpContext>(), new RouteData(), new ActionDescriptor()),
                    new IFilterMetadata[] { filter, },
                    new Dictionary<string, object>());

But when I call filter.OnActionExecuting(contextMock.Object) I get the following error:

Castle.DynamicProxy.InvalidProxyConstructorArgumentsException : Can not instantiate proxy of class: Microsoft.AspNet.Mvc.Filters.ActionExecutingContext.
      Could not find a constructor that would match given arguments:
      Microsoft.AspNet.Mvc.ActionContext
      Microsoft.AspNet.Mvc.Filters.IFilterMetadata[]
      System.Collections.Generic.Dictionary`2[System.String,System.Object]

How to test a filter that uses ModelState?

Megdal answered 14/4, 2016 at 16:56 Comment(0)
G
63

I just stumbled on the same problem and solved it in this way.

[Fact]
public void ValidateModelAttributes_SetsResultToBadRequest_IfModelIsInvalid()
{
    var validationFilter = new ValidationFilter();
    var modelState = new ModelStateDictionary();
    modelState.AddModelError("name", "invalid");

    var actionContext = new ActionContext(
        Mock.Of<HttpContext>(),
        Mock.Of<RouteData>(),
        Mock.Of<ActionDescriptor>(),
        modelState
    );

    var actionExecutingContext = new ActionExecutingContext(
        actionContext,
        new List<IFilterMetadata>(),
        new Dictionary<string, object>(),
        Mock.Of<Controller>()
    );

    validationFilter.OnActionExecuting(actionExecutingContext);

    Assert.IsType<BadRequestObjectResult>(actionExecutingContext.Result);
}
Gerardgerardo answered 14/9, 2016 at 13:12 Comment(1)
some of the usings : using System; using System.Net; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters;Russon
C
13

If someone was wondering how to do this when you inherit from IAsyncActionFilter

[Fact]
public async Task MyTest()
{
    var modelState = new ModelStateDictionary();

    var httpContextMock = new DefaultHttpContext();

    httpContextMock.Request.Query = new QueryCollection(new Dictionary<string, StringValues> {}); // if you are reading any properties from the query parameters

    var actionContext = new ActionContext(
        httpContextMock,
        Mock.Of<RouteData>(),
        Mock.Of<ActionDescriptor>(),
        modelState
    );

    var actionExecutingContext = new ActionExecutingContext(
        actionContext,
        new List<IFilterMetadata>(),
        new Dictionary<string, object>(),
        Mock.Of<Controller>()
    )
    {
        Result = new OkResult() // It will return ok unless during code execution you change this when by condition
    };

    Mymock1.SetupGet(x => x.SomeProperty).Returns("MySomething");
    Mymock2.Setup(x => x.GetSomething(It.IsAny<string>(), It.IsAny<string>())).ReturnsAsync(true);

    var context = new ActionExecutedContext(actionContext, new List<IFilterMetadata>(), Mock.Of<Controller>());

    await _classUnderTest.OnActionExecutionAsync(actionExecutingContext, async () => { return context; });

    actionExecutingContext.Result.Should().BeOfType<OkResult>();
}
Cristophercristy answered 4/7, 2019 at 13:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.