如何对会话状态进行单元测试
我试图弄清楚如何对我的对象进行单元测试,该对象将自身保留到会话状态。
控制器执行类似的操作...
[HttpPost]
public ActionResult Update(Account a)
{
SessionManager sm = SessionManager.FromSessionState(HttpContext.Current.Session);
if ( a.equals(sm.Account) )
sm.Account = a;
return View("View", a);
}
并且会话管理器将自身序列化为会话状态
public class SessionManager
{
public static SessionManager FromSessionState(HttpSessionStateBase state)
{
return state["sessionmanager"] as SessionManager ?? new SessionManager();
}
public Guid Id { get; set; }
public Account Account { get; set; }
public BusinessAssociate BusinessAssociate {get; set; }
}
我认为有两种方法进行单元测试...
在静态实例化器中传递会话状态引用,然后从中恢复。这将允许我模拟这个参数,一切都会很顺利。
公共静态SessionManager FromSessionState(HttpSessionStateWrapper会话) { 返回 session["sessionmanager"] 作为 SessionManager ??新的会话管理器(); }
- 为 SessionManager 创建一个模拟并使用它来测试控制器。
I'm trying to figure out how to unit test my object that persists itself to session state.
The controller does something like...
[HttpPost]
public ActionResult Update(Account a)
{
SessionManager sm = SessionManager.FromSessionState(HttpContext.Current.Session);
if ( a.equals(sm.Account) )
sm.Account = a;
return View("View", a);
}
And the session manager serializes itself to session state
public class SessionManager
{
public static SessionManager FromSessionState(HttpSessionStateBase state)
{
return state["sessionmanager"] as SessionManager ?? new SessionManager();
}
public Guid Id { get; set; }
public Account Account { get; set; }
public BusinessAssociate BusinessAssociate {get; set; }
}
I'm thinking that there are two approaches to unit testing...
in the static instantiator pass in the session state reference and then restore from that. this would allow me to mock this parameter and everything would be golden.
public static SessionManager FromSessionState(HttpSessionStateWrapper session)
{
return session["sessionmanager"] as SessionManager ?? new SessionManager();
}create a mock for the SessionManager and use that for testing of the controller.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里有一个想法: 创建一个会话管理器“持久性”接口:
创建一个基于 HTTPSession 的持久性实现:
现在让您的控制器依赖于
ISessionManagerPersist
。您可以在测试期间注入存根ISessionManagerPersist
并在生产期间使用基于会话的存根。如果您决定将其保留在其他地方(例如数据库或其他东西),这还有一个好处,即不需要进行更改。只需实现一个新的ISessionManagerPersist
。Here's an idea: Create a session manager "persistence" interface:
Create an HTTPSession-based implementation of your persistence:
Now make your controller have a dependency on
ISessionManagerPersist
. You can inject a stubISessionManagerPersist
during testing and use the session-based one during production. This also has the benefit of not needing changes if you decided to persist somewhere else (like a DB or something). Just implement a newISessionManagerPersist
.目前,此控制器操作很难进行单元测试,因为它调用依赖于
HttpContext.Current
的静态方法。虽然第一种方法似乎适用于第二种方法,但您仍然需要将会话状态传递到某处,以便管理器可以使用它。另外,不要传递HttpSessionStateWrapper
,而是使用HttpSessionStateBase
。Currently this controller action is very difficult to unit test as it calls a static method that is relying on
HttpContext.Current
. While the first approach seems to work in the second you still need to pass the session state somewhere so that the manager could work with it. Also don't passHttpSessionStateWrapper
, useHttpSessionStateBase
instead.