Tuesday, 13 August 2013

How do I inject principal for WebApi tesing?

How do I inject principal for WebApi tesing?

The MVC4 WebApi includes a User object of type
System.Security.Principal.IPrincipal which represents the actual user
logged on to the web server hosting the application. This is a very useful
objects since it allows to do things like
Comment = Ctx.Comment.Where(a => a.userProfileId == User.Identity.Name)
.FirstOrDefault();
Actually that code is in a repository but that is a different story. I am
writing some tests and I want to be able to mock the principal.
Unfortunately the 'User' is already instantiated for me so I don't know
how to inject my version. I am using AutoFac for IoC. Could someone please
shed some light on this problem?

No comments:

Post a Comment