如何用Moq模拟ActionExecutingContext? [英] How to mock ActionExecutingContext with Moq?

查看:61
本文介绍了如何用Moq模拟ActionExecutingContext?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测试以下过滤器:

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);
            }
        }
    }
}

我正在尝试使用Moq来模拟ActionFilterAttribute.

I am trying to mock the ActionFilterAttribute using Moq.

我正在使用Mvc 6.0.0-rc1

I am using Mvc 6.0.0-rc1

我的第一次尝试:

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

但是我无法覆盖ModelState.它看起来是只读的:

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

然后我尝试:

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

但是当我呼叫filter.OnActionExecuting(contextMock.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]

如何测试使用ModelState的过滤器?

How to test a filter that uses ModelState?

推荐答案

我偶然发现了相同的问题,并以此方式解决了该问题.

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);
}

这篇关于如何用Moq模拟ActionExecutingContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆