我有一个web服务,我试图单元测试。在服务中,它从HttpContext中提取了几个值,如下所示:
m_password = (string)HttpContext.Current.Session["CustomerId"];
m_userID = (string)HttpContext.Current.Session["CustomerUrl"];
在单元测试中,我使用一个简单的工作请求创建上下文,如下所示:
SimpleWorkerRequest request = new SimpleWorkerRequest("", "", "", null, new StringWriter());
HttpContext context = new HttpContext(request);
HttpContext.Current = context;
然而,每当我试图设置HttpContext.Current.Session的值时
HttpContext.Current.Session["CustomerId"] = "customer1";
HttpContext.Current.Session["CustomerUrl"] = "customer1Url";
我得到空引用异常,说HttpContext.Current.Session是空的。
是否有方法在单元测试中初始化当前会话?
从不嘲弄。从来没有!解决方法非常简单。为什么要伪造HttpContext这样一个美丽的作品呢?
下推会话!(这句话对我们大多数人来说已经足够理解了,但下面会详细解释)
(字符串)HttpContext.Current.Session(“CustomerId”);是我们现在访问它的方式。将其更改为
_customObject.SessionProperty("CustomerId")
当从test调用时,_customObject使用替代存储(DB或云键值[http://www.kvstore.io/])
但是当从实际应用程序调用时,_customObject使用Session。
这是怎么做到的?嗯…依赖注入!
因此test可以设置会话(地下),然后调用应用程序方法,就好像它对会话一无所知一样。然后测试秘密地检查应用程序代码是否正确地更新了会话。或者应用程序是否基于测试设置的会话值进行操作。
事实上,我们最终还是嘲笑了,尽管我说过:“永远不要嘲笑”。因为我们忍不住溜到下一个规则,“嘲笑最不疼的地方!”模拟巨大的HttpContext或模拟一个小的会话,哪个伤害最小?别问我这些规矩是从哪来的。我们就说常识吧。这里有一篇关于不要嘲笑单元测试会杀死我们的有趣文章
试试这个方法。
public static HttpContext getCurrentSession()
{
HttpContext.Current = new HttpContext(new HttpRequest("", ConfigurationManager.AppSettings["UnitTestSessionURL"], ""), new HttpResponse(new System.IO.StringWriter()));
System.Web.SessionState.SessionStateUtility.AddHttpSessionStateToContext(
HttpContext.Current, new HttpSessionStateContainer("", new SessionStateItemCollection(), new HttpStaticObjectsCollection(), 20000, true,
HttpCookieMode.UseCookies, SessionStateMode.InProc, false));
return HttpContext.Current;
}
我们必须通过使用HttpContextManager来模拟HttpContext,并从我们的应用程序和单元测试中调用工厂
public class HttpContextManager
{
private static HttpContextBase m_context;
public static HttpContextBase Current
{
get
{
if (m_context != null)
return m_context;
if (HttpContext.Current == null)
throw new InvalidOperationException("HttpContext not available");
return new HttpContextWrapper(HttpContext.Current);
}
}
public static void SetCurrentContext(HttpContextBase context)
{
m_context = context;
}
}
然后替换所有对HttpContext的调用。当前与HttpContextManager。当前和已获得相同的方法。然后在测试时,您还可以访问HttpContextManager并模拟您的期望
这是一个使用Moq的例子:
private HttpContextBase GetMockedHttpContext()
{
var context = new Mock<HttpContextBase>();
var request = new Mock<HttpRequestBase>();
var response = new Mock<HttpResponseBase>();
var session = new Mock<HttpSessionStateBase>();
var server = new Mock<HttpServerUtilityBase>();
var user = new Mock<IPrincipal>();
var identity = new Mock<IIdentity>();
var urlHelper = new Mock<UrlHelper>();
var routes = new RouteCollection();
MvcApplication.RegisterRoutes(routes);
var requestContext = new Mock<RequestContext>();
requestContext.Setup(x => x.HttpContext).Returns(context.Object);
context.Setup(ctx => ctx.Request).Returns(request.Object);
context.Setup(ctx => ctx.Response).Returns(response.Object);
context.Setup(ctx => ctx.Session).Returns(session.Object);
context.Setup(ctx => ctx.Server).Returns(server.Object);
context.Setup(ctx => ctx.User).Returns(user.Object);
user.Setup(ctx => ctx.Identity).Returns(identity.Object);
identity.Setup(id => id.IsAuthenticated).Returns(true);
identity.Setup(id => id.Name).Returns("test");
request.Setup(req => req.Url).Returns(new Uri("http://www.google.com"));
request.Setup(req => req.RequestContext).Returns(requestContext.Object);
requestContext.Setup(x => x.RouteData).Returns(new RouteData());
request.SetupGet(req => req.Headers).Returns(new NameValueCollection());
return context.Object;
}
然后在单元测试中使用它,我在Test Init方法中调用它
HttpContextManager.SetCurrentContext(GetMockedHttpContext());
然后,您可以在上面的方法中添加来自Session的预期结果,您希望它可以用于您的web服务。
你可以通过创建一个新的HttpContext来“伪造”它,就像这样:
http://www.necronet.org/archive/2010/07/28/unit-testing-code-that-uses-httpcontext-current-session.aspx
我把这段代码放到一个静态helper类中,如下所示:
public static HttpContext FakeHttpContext()
{
var httpRequest = new HttpRequest("", "http://example.com/", "");
var stringWriter = new StringWriter();
var httpResponse = new HttpResponse(stringWriter);
var httpContext = new HttpContext(httpRequest, httpResponse);
var sessionContainer = new HttpSessionStateContainer("id", new SessionStateItemCollection(),
new HttpStaticObjectsCollection(), 10, true,
HttpCookieMode.AutoDetect,
SessionStateMode.InProc, false);
httpContext.Items["AspSession"] = typeof(HttpSessionState).GetConstructor(
BindingFlags.NonPublic | BindingFlags.Instance,
null, CallingConventions.Standard,
new[] { typeof(HttpSessionStateContainer) },
null)
.Invoke(new object[] { sessionContainer });
return httpContext;
}
或者不是使用反射来构造新的httpessionstate实例,你可以将你的httpessionstatecontainer附加到HttpContext(根据Brent M. Spell的评论):
SessionStateUtility.AddHttpSessionStateToContext(httpContext, sessionContainer);
然后你可以像这样在单元测试中调用它:
HttpContext.Current = MockHelper.FakeHttpContext();
Milox解决方案比IMHO接受的解决方案更好,但我在处理url时遇到了一些问题。
我做了一些改变,使它能正常工作与任何url和避免反射。
public static HttpContext FakeHttpContext(string url)
{
var uri = new Uri(url);
var httpRequest = new HttpRequest(string.Empty, uri.ToString(),
uri.Query.TrimStart('?'));
var stringWriter = new StringWriter();
var httpResponse = new HttpResponse(stringWriter);
var httpContext = new HttpContext(httpRequest, httpResponse);
var sessionContainer = new HttpSessionStateContainer("id",
new SessionStateItemCollection(),
new HttpStaticObjectsCollection(),
10, true, HttpCookieMode.AutoDetect,
SessionStateMode.InProc, false);
SessionStateUtility.AddHttpSessionStateToContext(
httpContext, sessionContainer);
return httpContext;
}