Wie verspotte ich ActionExecutingContext mit Moq?
Ich versuche den folgenden Filter zu testen:
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);
}
}
}
}
Ich versuche, das ActionFilterAttribute mit Moq zu verspotten.
Ich benutze Mvc 6.0.0-rc1
Mein erster Versuch:
var context = new ActionExecutingContext(
new ActionContext(Mock.Of<HttpContext>(), new RouteData(), new ActionDescriptor()),
new IFilterMetadata[] { filter, },
new Dictionary<string, object>(),
controller: new object());
Aber ich konnte den ModelState nicht überschreiben. Es scheint nur lesbar zu sein:https: //github.com/aspnet/Mvc/blob/6.0.0-rc1/src/Microsoft.AspNet.Mvc.Abstractions/ActionContext.cs#L2
Dann habe ich versucht:
var contextMock = new Mock<ActionExecutingContext>(
new ActionContext(Mock.Of<HttpContext>(), new RouteData(), new ActionDescriptor()),
new IFilterMetadata[] { filter, },
new Dictionary<string, object>());
Aber wenn ich @ anrufilter.OnActionExecuting(contextMock.Object)
Ich erhalte den folgenden Fehler:
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]
Wie teste ich einen Filter, der ModelState verwendet?