单元测试 - Session对象? [英] Unit Testing - Session object?

查看:224
本文介绍了单元测试 - Session对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近实施的工作模式的一个单元,并作为一个环境,我们使用的是更单元测试。目前执行写入的写入会话的会话帮手。我如何单元关于会话测试这些方面?我应该做一个存储库模式? (混凝土会话实现和具体的模拟实现库接口)这是怎么回事正常完成?

I have recently Implemented a unit of work pattern, and as an environment we are using more unit testing. Currently the implementation writes into a session helper that writes to session. How do I unit test these aspects in regard to the session? Should I make a repository pattern? (repository interface with concrete session implementation and concrete mock implementation) How is this normally done?

我知道有可能不止一个接近这种方式,但我只是寻找一些建议。

I know there is probably more than one way of approaching this, but I am just looking for some advice.

推荐答案

有基本上都是这样做的两种方式。

There are basically two ways of doing this.

假设你正在使用.NET 3.5或向上。更改您的实现采取HttpSessionStateBase对象作为构造函数的参数,然后你可以嘲笑这种实现 - 有关于如何做这个网上一些教程。然后,您可以使用IoC容器在应用程序开始这样组装起来或者做类似(穷人的依赖注入):

Assuming you are using .NET 3.5 or up. Change your implementation to take the HttpSessionStateBase object as a constructor parameter, you can then mock this implementation - there's a few tutorials online on how to do this. You can then use an IoC container to wire this up at app start or do something like (poor man's dependency injection):

public class MyObjectThatUsesSession
{
    HttpSessionStateBase _session;

    public MyObjectThatUsesSession(HttpSessionStateBase sesssion)
    {
         _session = session ?? new HttpSessionStateWrapper(HttpContext.Current.Session);
    }

    public MyObjectThatUsesSession() : this(null)
    {}
}

另外,也许更好一点,更灵活的设计将是在另一个对象与会话包裹你的互动,创建一个测试缝。然后,您可以稍后将其更改为一个数据库,饼干或基于缓存实现。是这样的:

Alternatively, and probably a bit better and more flexible design would be to create a test seam by wrapping your interaction with session in another object. You could then change this to a database, cookie or cache based implementation later. Something like:

public class MyObjectThatUsesSession
{
    IStateStorage _storage;

    public MyObjectThatUsesSession(IStateStorage storage)
    {
         _storage= storage ?? new SessionStorage();
    }

    public MyObjectThatUsesSession() : this(null)
    {}

    public void DoSomethingWithSession()
    {
        var something = _storage.Get("MySessionKey");
        Console.WriteLine("Got " + something);
    }
}

public interface IStateStorage
{
    string Get(string key);
    void Set(string key, string data);
}

public class SessionStorage : IStateStorage
{
    //TODO: refactor to inject HttpSessionStateBase rather than using HttpContext.

    public string Get(string key)
    {
       return HttpContext.Current.Session[key];
    }

    public string Set(string key, string data)
    {
       HttpContext.Current.Session[key] = data;
    }
}

您就可以使用最小起订量来创建一个模拟IStateStorage实施为您的测试或创建一个简单的基于字典的实现。

You can then use Moq to create a mock IStateStorage implementation for your tests or create a simple dictionary based implementation.

希望有所帮助。

这篇关于单元测试 - Session对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆