【问题标题】:Migrate Custom Filter Attribute Unit Test from .NET Framework to .NET Core 2.1将自定义筛选器属性单元测试从 .NET Framework 迁移到 .NET Core 2.1
【发布时间】:2019-01-09 11:12:26
【问题描述】:

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

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

    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 的正确方法是什么?

或者这是在 .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 Core 版本中开发该属性的方式(它按预期工作,但我只需要为它重新实现单元测试。

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

【问题讨论】:

  • 您真正需要的是ActionExecutingContext 用于您的属性测试。

标签: c# .net unit-testing asp.net-core


【解决方案1】:

鉴于该属性的 .Net Core 版本,您实际需要的是一个 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

【讨论】:

  • 我会试一试
  • Nkosi - 您能否更新您的答案以包括使用 MSTest 时的断言应该是什么?断言在 NUnit 之上吗?我正在为我的测试框架使用 MS Test
  • 我用过-Assert.IsInstanceOfType(context.Result, typeof(BadRequestObjectResult));
  • Nkosi - 如果验证过滤器上没有错误,我将如何测试它是否返回成功响应?
  • 您断言上下文的结果为 null,这将断言为您最初提供的属性代码调用了 if (actionContext.ModelState.IsValid) return;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多