1

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.

Community
  • 1
  • 1
mmeasor
  • 459
  • 3
  • 19

2 Answers2

0

In order for me to stub the IPrincipal I had to fake System which in turn added mscorlib.fakes as well. This then enabled me to use System.Security.Principal.Fakes.StubIPrincipal in my code.

I then stubbed User.Identity.Name by doing the following:

using (AccountController controller = new AccountController())
{
    StubHttpContextBase stubHttpContext = new StubHttpContextBase();

    controller.ControllerContext = new ControllerContext(stubHttpContext, new RouteData(), controller);

    StubIPrincipal principal = new StubIPrincipal();
    principal.IdentityGet = () =>
    {
        return new StubIIdentity 
        { 
            NameGet = () => "bob" 
        };
    };
    stubHttpContext.UserGet = () => principal;
}
amurra
  • 15,221
  • 4
  • 70
  • 87
0

You can mock HttpContext.Current.User without using Microsoft Fakes (or any mocking library, for that matter). See this previous question (disclaimer: answered by me): Mock HttpContext.Current in Test Init Method

Community
  • 1
  • 1
Richard Szalay
  • 83,269
  • 19
  • 178
  • 237