mercredi 7 octobre 2015

how to mock data with extension methods?

I want test this method. Not exactly this because it does not make sense to test this, but this is simple version of what I need.

public bool Test()
{
    var authenticationScheme = Context.GetCurrentAuthenticationScheme();
    return authenticationScheme == "google";
}

GetCurrentAuthenticationScheme is extension method.

public static string GetCurrentAuthenticationScheme(this HttpContext context)
{

    var protector = context.ApplicationServices
                           .GetDataProtector(nameof(CookieStateHandlerExtensions));
    return protector.Unprotect(context.Request
                                      .Cookies[CURRENTAUTHENTICATIONSCHEMECOOKIE]
                                      .FirstOrDefault());
}

GetDataProtector is also extension method from "Microsoft.AspNet.DataProtection"
Unprotect is extension method from same namespace.

My question is how to mock this(using Moq) so that GetCurrentAuthenticationScheme() return string I want?

Aucun commentaire:

Enregistrer un commentaire