asp.netasp.net-mvc-3wiffakeiteasy

How fake System.Security.Principal.IIdentity using FakeItEasy?


In my app users loging in via WIF. User's credentials is stored in System.Security.Principal.IIdentity. Now i want to test CreateUser() method. I have to in some way fake that object. I try to do something like this: -extract method which returns that object:

public IIdentity GetIdentity()
{
    return Thread.CurrentPrincipal.Identity;
}

and then in test file do something like this:

    var identity = new GenericIdentity("mymail@mydomain.com");
    A.CallTo(() => _userRepository.GetIdentity()).Returns(identity);

But it doesn't work. Any ideas how to do that in the best way?


Solution

  • Have a look at WIF's ClaimsPrincipalHttpModule. It will transform a Windows identity into an IClaimsPrincipal without needing an STS.

    Also have a look at SelfSTS which is an extremely useful tool.