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

后端 未结 6 1704
我寻月下人不归
我寻月下人不归 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:06

    I've been banging my head against a brick wall over this also. I tried contextUtil but kept getting a null reference exception. I found out how to call an actionFilter in this post N.B. The actionFilter wasn't being invoked when using a Mock instance of the filter, I had to use the real object. HTH

    Specifically:

    var httpActionContext = new HttpActionContext
    {
        ControllerContext = new HttpControllerContext
        {
            Request = requestMessage
        }
    };
    
    //call filter
    var filter = new FooFilter();
    filter.OnActionExecuting(httpActionContext);
    

提交回复
热议问题