将自定义筛选器属性单元测试从 .NET Framework 迁移到 .NET Core 2.1 [英] Migrate Custom Filter Attribute Unit Test from .NET Framework to .NET Core 2.1

查看:34
本文介绍了将自定义筛选器属性单元测试从 .NET Framework 迁移到 .NET Core 2.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 .NET Framework 4.7.2 MVC 应用程序,我要迁移到 .NET Core 2.1 应用程序.到目前为止一切正常,但是我无法获得测试我在 .NET Core 版本中编写的自定义属性的单元测试之一.

I have a .NET Framework 4.7.2 MVC App that I am migrating to a .NET Core 2.1 App. Everything is working so far however I cannot get one of the Unit Tests that was testing a custom Attribute I wrote working in the .NET Core Version.

这是框架版本中属性测试的代码

This is the code for the Attribute Test in the Framework Version

    private HttpActionContext _successContext;
    private HttpActionContext _failContext;
    private HttpControllerContext _controllerContext;

    [TestInitialize]
    public void Initialize()
    {
        _controllerContext = new HttpControllerContext
        {
            Request = new HttpRequestMessage(HttpMethod.Post, string.Empty)
            {
                Content = new ObjectContent(typeof(string), string.Empty, new JsonMediaTypeFormatter())
            }
        };

        _successContext = new HttpActionContext {ControllerContext = _controllerContext};
        _successContext.ModelState.Add("TestField", new ModelState());

        _failContext = new HttpActionContext { ControllerContext = _controllerContext };
        _failContext.ModelState.Add("TestField", new ModelState());
        _failContext.ModelState.AddModelError("TestField", "Test error message");

    }

在 .NET Core 版本中新建 HttpControllerContext 和 ActionContext 的正确方法是什么?

What is the correct way to new up a HttpControllerContext and ActionContext in .NET Core version?

或者这是在 .NET Core 中测试属性的错误方法?

Or is this the incorrect approach to test an Attribute in .NET Core?

这是被测属性的Frameowrk版本

This is the Frameowrk version of the Attribute that is under test

public class JsonValidationFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        if (actionContext.ModelState.IsValid) return;
        var errorMessages = actionContext.ModelState.Values
            .SelectMany(modelState => modelState.Errors.Select(x => x.ErrorMessage));
        actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, Json.Encode(errorMessages));
    }
}

这就是我在 .NET Core 版本中开发该属性的方式(它按预期工作,但我只需要为它重新实现单元测试.

This is how I have developed that attribute in .NET Core version (it is working as expected but I just need to re-implement Unit Test for it.

public class JsonValidationFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext actionContext)
    {
        if (actionContext.ModelState.IsValid) return;
        IEnumerable<string> errorMessages = actionContext.ModelState.Values.SelectMany(modelState => modelState.Errors.Select(x => x.ErrorMessage));
        actionContext.Result = new BadRequestObjectResult(errorMessages);
    }
}

推荐答案

鉴于该属性的 .Net Core 版本,您实际需要的是一个 ActionExecutingContext 用于您的属性测试

Given the .Net Core version of the attribute, what you actually need is a ActionExecutingContext for your attribute test

例如

// Arrange
var context = new ActionExecutingContext(
    new ActionContext
    {
        HttpContext = new DefaultHttpContext(),
        RouteData = new RouteData(),
        ActionDescriptor = new ActionDescriptor()
    },
    new List<IFilterMetadata>(),
    new Dictionary<string, object>(),
    new object());

context.ModelState.AddModelError("TestField", "Test error message");

var filter = new JsonValidationFilterAttribute();

// Act
filter.OnActionExecuting(context);

// Assert
Assert.IsInstanceOfType(context.Result, typeof(BadRequestObjectResult));
//context.Result.Should().BeOfType<BadRequestObjectResult>(); Fluent Assertions

这篇关于将自定义筛选器属性单元测试从 .NET Framework 迁移到 .NET Core 2.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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