I am trying to develop a PATCH endpoint in my web api application. In the update model I have attributes for required data and some custom attributes. Since I found out that the attribute validation is not executed as expected in PATCH endpoints I needed to trigger the validation with a method as follows
private void Validate(object model, Type type)
{
var validator = Configuration.Services.GetBodyModelValidator();
var metadataProvider = Configuration.Services.GetModelMetadataProvider();
HttpActionContext actionContext = new HttpActionContext(
ControllerContext, Request.GetActionDescriptor());
if (!validator.Validate(model, type, metadataProvider, actionContext, String.Empty))
{
throw new HttpResponseException(Request.CreateErrorResponse(
HttpStatusCode.BadRequest, actionContext.ModelState));
}
}
This logic works fine for me. The problem is that when I try to write a unit test for the PATCH endpoint the tests fails because Request.GetActionDescriptor()
returns null. Maybe I need to set up my controller in a different way. Any ideas? Maybe I need to setup the Request object somehow