I have a similar problem to this post...
How can use Microsoft Fakes to mock User.Identity.Name
When attempting to follow one of the answers given, I ran into a problem creating one of the objects, StubIPrincipal.
I already had the Fakes added for System.Web. I added the Fakes for System as well, following the post. When I still couldn't create the stub, I also added Fakes for System.Security, where IPrincipal lives. Still no luck creating the user.
Here is the method I am using to try and stub HttpContext, which is working. I just also need to be able to get at the HttpContext.User.Identity as well.
public static HttpContextBase FakeHttpContext(string url = "")
{
var cookies = new HttpCookieCollection();
var session = new StubHttpSessionStateBase();
var server = new StubHttpServerUtilityBase();
var items = new ListDictionary();
var user = new StubIPrincipal // Cannot resolve symbol 'StubIPrincipal'
{
};
var browser = new StubHttpBrowserCapabilitiesBase
{
IsMobileDeviceGet = () => false
};
var request = new StubHttpRequestBase
{
AppRelativeCurrentExecutionFilePathGet = () => url,
ApplicationPathGet = () => url,
CookiesGet = () => cookies,
UserAgentGet = () => "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11",
BrowserGet = () => browser,
UrlGet = () => !string.IsNullOrEmpty(url) ? new Uri(url) : null
};
var response = new StubHttpResponseBase
{
CookiesGet = () => cookies,
ApplyAppPathModifierString = s => s,
StatusCodeGet = () => (int)HttpStatusCode.OK
};
var context = new StubHttpContextBase
{
RequestGet = () => request,
ResponseGet = () => response,
SessionGet = () => session,
ServerGet = () => server,
ItemsGet = () => items
};
return context;
}
Another thing I tried was just manually creating a user, and adding it to the context.
System.Security.Principal.IPrincipal user;
var winId = new WindowsIdentity("u1332304");
user = new WindowsPrincipal(winId);
context.User = user;
When I do that I get a Not Implemented error on the context.User object.