Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mock GetExternalLoginInfoAsync() method

I have private property to access AuthenticationManager:

private IAuthenticationManager AuthenticationManager => HttpContext.GetOwinContext().Authentication;

I want to mock the method GetExternalLoginInfoAsync() in order to receive fake ExternalLoginInfo object:

ExternalLoginInfo loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();

Both GetOwinContext() and GetExternalLoginInfoAsync() methods are extension methods. Is it possible to see source code of these methods to see how they works?

like image 778
John Doe Avatar asked Jun 07 '26 19:06

John Doe


1 Answers

Both GetOwinContext() and GetExternalLoginInfoAsync() methods are extension methods. Is it possible to see source code of these methods to see how they works?

Sure, one way is to use ILSpy. For example:

/// <summary>
///     Extracts login info out of an external identity
/// </summary>
/// <param name="manager"></param>
/// <returns></returns>
public static async Task<ExternalLoginInfo> GetExternalLoginInfoAsync(this IAuthenticationManager manager)
{
    if (manager == null)
    {
        throw new ArgumentNullException("manager");
    }
    return AuthenticationManagerExtensions.GetExternalLoginInfo(await TaskExtensions.WithCurrentCulture<AuthenticateResult>(manager.AuthenticateAsync("ExternalCookie")));
}

However, in my opinion this is just a Rabbit hole. Instead, you should be using Dependency Injection to inject a IAuthenticationManager into your controller. Then you can simply Mock it and the methods you need instead of testing internal microsoft code.

like image 56
Erik Philips Avatar answered Jun 10 '26 08:06

Erik Philips



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!