vendredi 18 décembre 2015

How do I stub/mock AuthenticationManager in MVC 6?

I'm trying to unit test an MVC 6 controller that directly interacts with this.HttpContext.Authentication for authentication. I'm not using the Identity Framework, but rather am interacting with Cookie Authentication middleware directly.

Following some examples I've seen by MS, I've been using DefaultHttpContext for testing purposes. The problem is that the AuthenticationManager property on DefaultHttpContext is read-only so I'm unsure how I can mock it.

If I don't mock it, I receive an error stating "No authentication handler is configured to handle the scheme: Cookies" in my tests when calling:

HttpContext.Authentication.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal, authProperties);

Aucun commentaire:

Enregistrer un commentaire