Some of the actions that we are writing in Asp.Net MVC contains logic which uses data from the User context eg. user authentication or user name. Controller base class contains User property which is the instance of IPrincipal with two important properties.
The IPrincipal is taken from the HttpContext
So if your action is using the User property directly (you can always wrap this property inside a class that implements mockable Interface ) there is a problem with unit testing. In a isolated enviroment like test case , Controller doesn’t have the HttpContext. It is an external dependency.
HttpContext is retrieved from the ControllerContext
In order to fake the User property First we need to create a fake ControllerContext. To create it we need HttpContext which also needs HttpRequest. User is created from IPrincipal and IIdentity , with those classes we can create a Stub inside the HttpContext.
Yep , It’s quite complicated. Fortunately MvcContrib Library helps a little by providing classes that are faking IIdentity and IPrincipal
If you want to create a fake user you just need to write
When creating FakeIdentity userName parametr is really important. If you want ,not authenticated user , pass Empty String as a parameter
Yey , it’s the end ! Check out this simple graph.
Using this information , I have implemented simple TestHelper with methods to generate Fake ControlleContext with faked User.
Hope this sample helps.Tweet