如何对会话状态进行单元测试

发布于 2024-09-24 02:44:08 字数 1058 浏览 6 评论 0原文

我试图弄清楚如何对我的对象进行单元测试,该对象将自身保留到会话状态。

控制器执行类似的操作...

    [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; }
    }

我认为有两种方法进行单元测试...

  1. 在静态实例化器中传递会话状态引用,然后从中恢复。这将允许我模拟这个参数,一切都会很顺利。

    公共静态SessionManager FromSessionState(HttpSessionStateWrapper会话) { 返回 session["sessionmanager"] 作为 SessionManager ??新的会话管理器(); }

  2. 为 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...

  1. 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();
    }

  2. create a mock for the SessionManager and use that for testing of the controller.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

久而酒知 2024-10-01 02:44:08

这里有一个想法: 创建一个会话管理器“持久性”接口:

public interface ISessionManagerPersist
{
    SessionManager Load();
    void Save(SessionManager sessionManager);
}

创建一个基于 HTTPSession 的持久性实现:

public class SessionStatePersist : ISessionManagerPersist
{
    public SessionManager Load()
    {
        return HttpContext.Current.Session["sessionmanager"] as SessionManager ?? new SessionManager();
    }

    public void Save(SessionManager sessionManager)
    {
        HttpContext.Current.Session["sessionmanager"] = sessionManager;
    }
}

现在让您的控制器依赖于 ISessionManagerPersist。您可以在测试期间注入存根 ISessionManagerPersist 并在生产期间使用基于会话的存根。如果您决定将其保留在其他地方(例如数据库或其他东西),这还有一个好处,即不需要进行更改。只需实现一个新的ISessionManagerPersist

Here's an idea: Create a session manager "persistence" interface:

public interface ISessionManagerPersist
{
    SessionManager Load();
    void Save(SessionManager sessionManager);
}

Create an HTTPSession-based implementation of your persistence:

public class SessionStatePersist : ISessionManagerPersist
{
    public SessionManager Load()
    {
        return HttpContext.Current.Session["sessionmanager"] as SessionManager ?? new SessionManager();
    }

    public void Save(SessionManager sessionManager)
    {
        HttpContext.Current.Session["sessionmanager"] = sessionManager;
    }
}

Now make your controller have a dependency on ISessionManagerPersist. You can inject a stub ISessionManagerPersist 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 new ISessionManagerPersist.

↙厌世 2024-10-01 02:44:08

目前,此控制器操作很难进行单元测试,因为它调用依赖于 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 pass HttpSessionStateWrapper, use HttpSessionStateBase instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文