考虑.NET程序集中的方法:
public static string GetSecurityContextUserName()
{
//extract the username from request
string sUser = HttpContext.Current.User.Identity.Name;
//everything after the domain
sUser = sUser.Substring(sUser.IndexOf("\\") + 1).ToLower();
return sUser;
}
Run Code Online (Sandbox Code Playgroud)
我想使用Moq框架从单元测试中调用此方法.该程序集是webforms解决方案的一部分.单元测试看起来像这样,但我错过了Moq代码.
//arrange
string ADAccount = "BUGSBUNNY";
string fullADName = "LOONEYTUNES\BUGSBUNNY";
//act
//need to mock up the HttpContext here somehow -- using Moq.
string foundUserName = MyIdentityBL.GetSecurityContextUserName();
//assert
Assert.AreEqual(foundUserName, ADAccount, true, "Should have been the same User Identity.");
Run Code Online (Sandbox Code Playgroud)
问题:
MyIdentityBL.GetSecurityContextUserName()?