如何在DotNet Core中编写CustomActionFilter的单元测试

时间:2019-01-11 19:09:16

标签: unit-testing asp.net-web-api .net-core

我有自定义操作过滤器,我想在dotnet核心weabpi中为此编写单元测试。

请帮助。

public class ApplicationIdValidationAttribute: IActionFilter
{
    public void OnActionExecuting(ActionExecutingContext context)
    {
        var param = context.ActionArguments["Id"];

        var modelState = context.ModelState;
        if (param!=null && string.IsNullOrEmpty(param.ToString()))
        {
            modelState.AddModelError("id", "Application id is required");
            context.Result = new BadRequestObjectResult(new ModelStateRequestValidationAdaptor(modelState));
            return;
        }

        if (!Guid.TryParse(param?.ToString(), out Guid applicationGuid))
        {
            modelState.AddModelError("id", "Application id is not a valid Guid");
            context.Result = new BadRequestObjectResult(new ModelStateRequestValidationAdaptor(modelState));
            return;
        }
    }

    public void OnActionExecuted(ActionExecutedContext context)
    {
    }
}

0 个答案:

没有答案