How can you unit test an Action Filter in ASP.NET Web Api?

后端 未结 6 1713
我寻月下人不归
我寻月下人不归 2020-12-22 22:43

I was looking to add an Action Filter to my service to handle adding link data to the response message. I have found that I need to mock HttpActionExecutedContext but it\'s

6条回答
  •  情深已故
    2020-12-22 23:09

    Just new one up.

    private HttpActionContext CreateExecutingContext()
    {
        return new HttpActionContext { ControllerContext = new HttpControllerContext {   Request = new HttpRequestMessage() } };
    }
    
    private HttpActionExecutedContext CreateExecutedContextWithStatusCode(HttpStatusCode statusCode)
    {
        return new HttpActionExecutedContext
        {
            ActionContext = new HttpActionContext
            {
                ControllerContext = new HttpControllerContext
                {
                    Request = new HttpRequestMessage()
                }
            },
            Response = new HttpResponseMessage
            {
                StatusCode = statusCode,
                Content = new StringContent("blah")
            }
        };
    }
    

提交回复
热议问题