0

I'm trying to mock the HttpActioncontext,But nothing seems to work out. I'm using nunit 2.7.0 framework . Please help.

My Controller Method:

    public HttpResponseMessage Put([FromBody] EditAccountRequest editAccountRequest)
    {
        **var username = ActionContext.Request.Headers.FirstOrDefault(a => a.Key == "username");**
        
       ...........
    }
  • Does this answer your question? [Mock HttpContext.Current in Test Init Method](https://stackoverflow.com/questions/4379450/mock-httpcontext-current-in-test-init-method) – madoxdev Apr 29 '21 at 12:41
  • 1
    What do you mean by nothing seems to work? Please add details to your question. Also, welcome to [Stack Overflow](https://stackoverflow.com/help/how-to-ask). – Grizzlly Apr 29 '21 at 12:42
  • [Test] public void PutTest() { EditAccountRequest editAccountRequest = new EditAccountRequest(); editAccountRequest.Mode = 1; HttpContext.Current = new HttpContext(new HttpRequest(null, "http://tempuri.org", null), new HttpResponse(null)); HttpResponseMessage result = accountController.Put(editAccountRequest); Assert.IsNotNull(result); } – San Krish Apr 29 '21 at 12:49
  • 1
    @SanKrish [edit] that last comment into the original post. We wont be able to fully understand it in the comments. – Nkosi Apr 29 '21 at 12:51
  • @Nkosi Below code I have tried ,But no luck . HttpContext.Current = new HttpContext(new HttpRequest(null, "tempuri.org", null), new HttpResponse(null)); – San Krish Apr 29 '21 at 13:34

0 Answers0