在我的方法中,我通过使用" HttpContext.Current.User "来获取当前用户。就像这个
public class Test
{
public UserPrincipal GetUserContext()
{
System.Security.Principal.IPrincipal user = HttpContext.Current.User;
return user;
}
}
在测试方法中,我正在这样访问,
[TestMethod()]
public void TestMethod()
{
Test objTest = new Test();
System.Security.Principal.IPrincipal user=objTest.GetUserContext()
}
在运行此测试方法时,由于" HttpContext.Current.User"为空
任何人都可以熟悉这个问题。 提前谢谢。
答案 0 :(得分:0)
此代码将创建假的HttpContext:
string UserName = "username";
HttpContext.Current = new HttpContext(
new HttpRequest("", "http://tempuri.org", ""),
new HttpResponse(new StringWriter()));
HttpContext.Current.User = new GenericPrincipal(
new GenericIdentity(UserName),new string[0]);