C# 将自定义筛选器属性单元测试从.NET Framework迁移到.NET Core 2.1

C# 将自定义筛选器属性单元测试从.NET Framework迁移到.NET Core 2.1,c#,.net,unit-testing,asp.net-core,C#,.net,Unit Testing,Asp.net Core,我有一个.NET Framework 4.7.2 MVC应用程序,我正在将其迁移到.NET Core 2.1应用程序。到目前为止,一切都正常,但是我无法获得一个单元测试,该单元测试测试了我在.NET核心版本中编写的自定义属性 这是框架版本中属性测试的代码 private HttpActionContext _successContext; private HttpActionContext _failContext; private HttpControllerConte

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

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

    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核心版本中更新HttpControllerContext和ActionContext的正确方法是什么

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

这是正在测试的属性的Frameowrk版本

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核心版本中开发该属性的方法(它按预期工作,但我只需要为它重新实现单元测试)

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);
    }
}
公共类JsonValidationFilterAttribute:ActionFilterAttribute
{
公共覆盖无效OnActionExecuting(ActionExecutingContext actionContext)
{
if(actionContext.ModelState.IsValid)返回;
IEnumerable errorMessages=actionContext.ModelState.Values.SelectMany(ModelState=>ModelState.Errors.Select(x=>x.ErrorMessage));
actionContext.Result=新的BadRequestObjectResult(errorMessages);
}
}

给定.Net核心版本的属性,您实际需要的是属性测试的
ActionExecutingContext

比如说

// 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
//排列
var context=新的ActionExecutingContext(
新行动背景
{
HttpContext=新的DefaultHttpContext(),
RoutedData=新RoutedData(),
ActionDescriptor=新的ActionDescriptor()
},
新列表(),
新字典(),
新对象());
context.ModelState.AddModelError(“测试字段”,“测试错误消息”);
var filter=新的JsonValidationFilterAttribute();
//表演
OnActionExecuting(上下文);
//断言
IsInstanceOfType(context.Result,typeof(BadRequestObjectResult));
//context.Result.Should().BeOfType();流畅的断言

您实际需要的是一个
ActionExecutingContext
用于属性测试。我会给它一个goNkosi-您可以更新您的答案,包括使用MSTest时断言应该是什么吗?断言是否在NUnit之上?我正在使用MS test作为我的测试框架-Assert.IsInstanceOfType(context.Result,typeof(BadRequestObjectResult));Nkosi-如果验证筛选器上没有错误,我如何测试它是否返回成功响应?您断言上下文的结果为null,这将断言
if(actionContext.ModelState.IsValid)return;
是为您最初提供的属性代码调用的