我有一个网络服务我正在尝试进行单元测试。在服务中,它从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
为空。
有没有办法在单元测试中初始化当前会话?
答案 0 :(得分:276)
你可以通过像这样创建一个新的HttpContext
来“伪造它”:
我已经把这段代码放在一个静态助手类上,如下所示:
public static HttpContext FakeHttpContext()
{
var httpRequest = new HttpRequest("", "http://stackoverflow/", "");
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;
}
或者不是使用反射来构建新的HttpSessionState
实例,而是可以将HttpSessionStateContainer
附加到HttpContext
(根据Brent M. Spell的评论):
SessionStateUtility.AddHttpSessionStateToContext(httpContext, sessionContainer);
然后你可以在你的单元测试中调用它,如:
HttpContext.Current = MockHelper.FakeHttpContext();
答案 1 :(得分:98)
我们必须使用HttpContext
模拟HttpContextManager
并在我们的应用程序中调用工厂以及单元测试
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.Current
替换对HttpContextManager.Current
的任何调用,并且可以访问相同的方法。然后,当您进行测试时,您还可以访问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());
然后,您可以在上面的方法中添加您希望Web服务可用的Session的预期结果。
答案 2 :(得分:42)
Milox solution胜过公认的恕我直言,但I had some problems with this implementation when handling urls with querystring。
我做了一些更改,以使其与任何网址一起正常工作,并避免反思。
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;
}
答案 3 :(得分:12)
如果您正在使用MVC框架,这应该可行。我使用了Milox's FakeHttpContext并添加了一些额外的代码行。这个想法来自这篇文章:
这似乎适用于MVC 5.我在早期版本的MVC中没有尝试过这个。
HttpContext.Current = MockHttpContext.FakeHttpContext();
var wrapper = new HttpContextWrapper(HttpContext.Current);
MyController controller = new MyController();
controller.ControllerContext = new ControllerContext(wrapper, new RouteData(), controller);
string result = controller.MyMethod();
答案 4 :(得分:11)
您可以尝试FakeHttpContext:
using (new FakeHttpContext())
{
HttpContext.Current.Session["CustomerId"] = "customer1";
}
答案 5 :(得分:7)
与我合作的答案是@Anthony写的,但你必须添加另一行
request.SetupGet(req => req.Headers).Returns(new NameValueCollection());
所以你可以使用它:
HttpContextFactory.Current.Request.Headers.Add(key, value);
答案 6 :(得分:6)
在asp.net Core / MVC 6 rc2中,您可以设置HttpContext
var SomeController controller = new SomeController();
controller.ControllerContext = new ControllerContext();
controller.ControllerContext.HttpContext = new DefaultHttpContext();
controller.HttpContext.Session = new DummySession();
rc 1
var SomeController controller = new SomeController();
controller.ActionContext = new ActionContext();
controller.ActionContext.HttpContext = new DefaultHttpContext();
controller.HttpContext.Session = new DummySession();
https://stackoverflow.com/a/34022964/516748
考虑使用Moq
new Mock<ISession>();
答案 7 :(得分:1)
试试这个:
// MockHttpSession Setup
var session = new MockHttpSession();
// MockHttpRequest Setup - mock AJAX request
var httpRequest = new Mock<HttpRequestBase>();
// Setup this part of the HTTP request for AJAX calls
httpRequest.Setup(req => req["X-Requested-With"]).Returns("XMLHttpRequest");
// MockHttpContextBase Setup - mock request, cache, and session
var httpContext = new Mock<HttpContextBase>();
httpContext.Setup(ctx => ctx.Request).Returns(httpRequest.Object);
httpContext.Setup(ctx => ctx.Cache).Returns(HttpRuntime.Cache);
httpContext.Setup(ctx => ctx.Session).Returns(session);
// MockHttpContext for cache
var contextRequest = new HttpRequest("", "http://localhost/", "");
var contextResponse = new HttpResponse(new StringWriter());
HttpContext.Current = new HttpContext(contextRequest, contextResponse);
// MockControllerContext Setup
var context = new Mock<ControllerContext>();
context.Setup(ctx => ctx.HttpContext).Returns(httpContext.Object);
//TODO: Create new controller here
// Set controller's ControllerContext to context.Object
并添加课程:
public class MockHttpSession : HttpSessionStateBase
{
Dictionary<string, object> _sessionDictionary = new Dictionary<string, object>();
public override object this[string name]
{
get
{
return _sessionDictionary.ContainsKey(name) ? _sessionDictionary[name] : null;
}
set
{
_sessionDictionary[name] = value;
}
}
public override void Abandon()
{
var keys = new List<string>();
foreach (var kvp in _sessionDictionary)
{
keys.Add(kvp.Key);
}
foreach (var key in keys)
{
_sessionDictionary.Remove(key);
}
}
public override void Clear()
{
var keys = new List<string>();
foreach (var kvp in _sessionDictionary)
{
keys.Add(kvp.Key);
}
foreach(var key in keys)
{
_sessionDictionary.Remove(key);
}
}
}
这将允许您使用会话和缓存进行测试。
答案 8 :(得分:1)
我正在寻找比上述选项更具侵略性的东西。最后我提出了一个俗气的解决方案,但它可能会让一些人快速移动。
首先我创建了一个 TestSession 类:
class TestSession : ISession
{
public TestSession()
{
Values = new Dictionary<string, byte[]>();
}
public string Id
{
get
{
return "session_id";
}
}
public bool IsAvailable
{
get
{
return true;
}
}
public IEnumerable<string> Keys
{
get { return Values.Keys; }
}
public Dictionary<string, byte[]> Values { get; set; }
public void Clear()
{
Values.Clear();
}
public Task CommitAsync()
{
throw new NotImplementedException();
}
public Task LoadAsync()
{
throw new NotImplementedException();
}
public void Remove(string key)
{
Values.Remove(key);
}
public void Set(string key, byte[] value)
{
if (Values.ContainsKey(key))
{
Remove(key);
}
Values.Add(key, value);
}
public bool TryGetValue(string key, out byte[] value)
{
if (Values.ContainsKey(key))
{
value = Values[key];
return true;
}
value = new byte[0];
return false;
}
}
然后我在控制器的构造函数中添加了一个可选参数。如果参数存在,请将其用于会话操作。否则,请使用HttpContext.Session:
class MyController
{
private readonly ISession _session;
public MyController(ISession session = null)
{
_session = session;
}
public IActionResult Action1()
{
Session().SetString("Key", "Value");
View();
}
public IActionResult Action2()
{
ViewBag.Key = Session().GetString("Key");
View();
}
private ISession Session()
{
return _session ?? HttpContext.Session;
}
}
现在我可以将 TestSession 注入控制器:
class MyControllerTest
{
private readonly MyController _controller;
public MyControllerTest()
{
var testSession = new TestSession();
var _controller = new MyController(testSession);
}
}
答案 9 :(得分:1)
永远不要嘲笑..永远不要!解决方案非常简单。为什么假装像HttpContext
这样美丽的创作呢?
推下会话! (这条线足以让我们大多数人理解,但在下面详细解释)
(string)HttpContext.Current.Session["CustomerId"];
是我们现在访问它的方式。将其更改为
_customObject.SessionProperty("CustomerId")
从test中调用时,_customObject使用备用存储(DB或云键值[http://www.kvstore.io/])
但是,当从真实应用程序调用时,_customObject
使用Session
。
这是怎么做到的?嗯...依赖注入!
因此,测试可以设置会话(地下),然后调用应用程序方法,就好像它对会话一无所知。然后测试秘密检查应用程序代码是否正确更新了会话。或者,如果应用程序的行为基于测试设置的会话值。
实际上,即使我说过,我们也最终嘲笑:&#34;从不嘲笑&#34;。因为我们无法帮助,但却陷入下一个规则,&#34;嘲笑它至少受伤的地方!&#34;。嘲弄巨大的HttpContext
或嘲笑一个微小的会话,这会伤害最少?不要问我这些规则来自哪里。我们只说常识。这是一篇关于不嘲笑as unit test can kills us
答案 10 :(得分:0)
答案 @Ro Hit 给了我很多帮助,但我错过了用户凭据,因为我不得不伪造用户进行身份验证单元测试。因此,让我描述一下我是如何解决它的。
根据this,如果添加方法
// using System.Security.Principal;
GenericPrincipal FakeUser(string userName)
{
var fakeIdentity = new GenericIdentity(userName);
var principal = new GenericPrincipal(fakeIdentity, null);
return principal;
}
然后追加
HttpContext.Current.User = FakeUser("myDomain\\myUser");
到您完成的TestSetup
方法的最后一行,添加了用户凭据并准备用于身份验证测试。
我还注意到您可能需要HttpContext中的其他部分,例如.MapPath()
方法。有一个FakeHttpContext,它是described here,可以通过NuGet安装。
答案 11 :(得分:0)
我找到了以下用于在HttpContext中指定用户的简单解决方案:https://forums.asp.net/post/5828182.aspx
答案 12 :(得分:0)
尝试这种方式。
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;
}