Unit Test Web Api 2 Mock User

You can set the user in ControllerContext.RequestContext.Principal:

controller.ControllerContext.RequestContext.Principal = new GenericPrincipal(new GenericIdentity("Bob", "Passport"), new[] {"managers"});

Or a shorthand equivalent:

controller.User = new GenericPrincipal(new GenericIdentity("Bob", "Passport"), new[] {"managers"});

Though it is an old post, but I would like to add my thought as well.

If you only want to mock User Identity no need to use any mock framework, as you cannot mock User Identity using mock framework(not atleast with moq).

Simply assign the HttpContext.current property as below

HttpContext.Current = new HttpContext(
    new HttpRequest("", "http://localhost", ""),
    new HttpResponse(null)
);

And the HttpContext.Current.User be like this

HttpContext.Current.User = 
           new GenericPrincipal(
               new GenericIdentity("name"),
               new []{"manager","Admin"}
           );

Now the HttpContext.Current.User.Identity.Name will be available in controller of web api.

If you want to mock other property or object use moq or any other framework. But mocking HttpContext is as simple as the above.