"HttpContext.Current.User" 为空,当通过单元测试方法访问时
"HttpContext.Current.User" is null, when it is access through unit test method
在我的方法中,我通过使用“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" 是空的
任何熟悉这个问题的人都可以。
提前致谢。
此代码将创建假的 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]);
在我的方法中,我通过使用“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" 是空的
任何熟悉这个问题的人都可以。 提前致谢。
此代码将创建假的 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]);