How can you mock the User.Identity [for unit testing] of the ASP.NET WebApi ApiControllers?
Note:
Some of the controller methods returns are aync [Task]
which run on a different thread
How can you mock the User.Identity [for unit testing] of the ASP.NET WebApi ApiControllers?
Note:
Some of the controller methods returns are aync [Task]
which run on a different thread
I had to do something similar for an MVC controller test. What I ended up doing was attaching a fake HttpContext to the controller and then attaching a GenericPrincipal to the fake context.
var myController = new SomeController();
var context = A.Fake<HttpContextBase>();
ControllerContext context = new ControllerContext( new RequestContext( context, new RouteData() ), myController );
myController.ControllerContext = context;
myController.HttpContext.User = new GenericPrincipal( new GenericIdentity( "bob" ), null );
I'm using FakeItEasy so the syntax for generating the fakes may be different for you.