Skip to content

Instantly share code, notes, and snippets.

@jovaneyck
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jovaneyck/8c81bf83e2e1dacd4548 to your computer and use it in GitHub Desktop.
Save jovaneyck/8c81bf83e2e1dacd4548 to your computer and use it in GitHub Desktop.
Working with Thread.CurrentPrinciple in unit tests by providing a very thin abstraction layer
public interface IPrincipalInformationProvider
{
bool IsLoggedIn { get; }
...
}
public class ThreadBasedPrincipalInformationProvider : IPrincipalInformationProvider
{
public bool IsLoggedIn
{
get { return Thread.CurrentPrincipal.Identity.IsAuthenticated; }
}
...
}
public class NotationController : ApiController
{
public NotationController(
IPrincipalInformationProvider principalInformationProvider) //Can inject a fake easily
{
_principalInformationProvider = principalInformationProvider;
}
[HttpGet]
[Route("Notations", Name = RouteNames.NOTATIONS_SEARCH)]
public HttpResponseMessage Get([FromUri] MobileSearchFilterParameter filter)
{
...
var searchResult = _manager.Search(filter, _principalInformationProvider.IsLoggedIn;
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment