Ich habe eine Klasse in einer Klassenbibliothek, die von Windows-Anwendung oder Webanwendung verwendet werden soll.UnitTest zum Simulieren der Assembly, auf die von einer WebApplication verwiesen wird (mit IPrincipal-Füllung)
ist es definiert als
public class CredentialProviderPipelineStep
{
internal static string GetDomainFullName(string friendlyName)
{
var context = new DirectoryContext(DirectoryContextType.Domain, friendlyName);
var domain = Domain.GetDomain(context);
return domain?.Name;
}
public static Task<IDictionary<string, object>> Action(IConfiguration configuration)
{
return Task.Run<IDictionary<string, object>>(() =>
{
var context = new Dictionary<string, object>
{
[Resources.DomainName] = GetDomainFullName(Environment.UserDomainName),
[Resources.DomainUser] = Environment.UserName
};
if (configuration.AppSettings[Resources.SSOApplicationId] == null)
throw new KeyNotFoundException(Resources.SSOApplicationId);
context[Resources.ApplicationId] =
Convert.ToInt32(configuration.AppSettings[Resources.SSOApplicationId]);
return context;
});
}
internal static IDictionary<string, object> GetUserAndDomain()
{
//It's to check if it's a web app, so I've to ask for principal data <see href="http://stackoverflow.com/questions/3179716/how-determine-if-application-is-web-application/"></see>
return Assembly.GetEntryAssembly() == null ? GetUserAndDomainFromWebApp() : GetUserAndDomainFromEnviorment();
}
private static IDictionary<string,object> GetUserAndDomainFromEnviorment()
{
var httpContext = System.Web.HttpContext.Current.User.Identity.Name;
string[] splitted = httpContext.Split('\\');
var result = new Dictionary<string, object>
{
[Resources.DomainName] = GetDomainFullName(Environment.UserDomainName),
[Resources.DomainUser] = Environment.UserName
};
return result;
}
private static IDictionary<string, object> GetUserAndDomainFromWebApp()
{
var result = new Dictionary<string, object>
{
[Resources.DomainName] = GetDomainFullName(Environment.UserDomainName),
[Resources.DomainUser] = Environment.UserName
};
return result;
}
}
Der kanonische Unittest (unter Verwendung von NUnit) deckt den GetUserAndDomainFromEnviorment Teil, aber wie kann ich einen Komponententest schreiben, die die Methode GetUserAndDomainFromWebApp abdecken? Also ist der System.Web.HttpContext.Current.User.Identity.Name gefüllt?
Dank
Welche VS-Edition verwenden Sie? – nozzleman