在ASP.net中模拟HttpSessionState进行单元测试

前端之家收集整理的这篇文章主要介绍了在ASP.net中模拟HttpSessionState进行单元测试前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经看到很多关于HttpSessionState和asp.net MVC的讨论.
我试图为asp.net应用程序编写测试,并想知道是否可以模拟HttpSessionState,如果是,怎么办?

我目前正在使用犀牛模型和Nunit

解决方法

吉尔伯特

也许我为你太迟了我使用MSpec,但我认为这些概念是相似的.我需要在被测控制器中模拟HttpContext的几个组件.

我开始使用以下类来模拟HttpContextBase中必要的(为了我的目的)组件.我只是把班里的必要的东西翻过来.您的需求将随控制器中所需的模拟而变化.一旦了解了模式,就可以根据需要添加嘲讽.

public class MockHttpContext : HttpContextBase 
{

    private readonly HttpRequestBase _request = new MockHttpRequest();
    private readonly HttpServerUtilityBase _server = new MockHttpServerUtilityBase();
    private HttpSessionStateBase _session = new MockHttpSession();

    public override HttpRequestBase Request
    {
        get { return _request; }
    }
    public override HttpServerUtilityBase Server
    {
        get { return _server; }
    }
    public override HttpSessionStateBase Session
    {
        get { return _session; }
    }
}

public class MockHttpRequest : HttpRequestBase
{
    private Uri _url = new Uri("http://www.mockrequest.moc/Controller/Action");

    public override Uri Url
    {
        get { return _url; }
    }
}

public class MockHttpServerUtilityBase : HttpServerUtilityBase
{
    public override string UrlEncode(string s)
    {
        //return base.UrlEncode(s);     
        return s;       // Not doing anything (this is just a Mock)
    }
}


public class MockHttpSession : HttpSessionStateBase
{
    // Started with sample https://stackoverflow.com/questions/524457/how-do-you-mock-the-session-object-collection-using-moq
    // from https://stackoverflow.com/users/81730/ronnblack

    System.Collections.Generic.Dictionary<string,object> _sessionStorage = new   System.Collections.Generic.Dictionary<string,object>();
    public override object this[string name]
    {
        get { return _sessionStorage[name]; }
        set { _sessionStorage[name] = value; }
    }

    public override void Add(string name,object value)
    {
        _sessionStorage[name] = value;
    }
}

这是我如何设置Controller Context以使用mocks(MSpec).这是为了对实际测试进行设置(测试来源于此类)

public abstract class BlahBlahControllerContext
{
    protected static BlahBlahController controller;

    Establish context = () =>
    {
        controller = new BlahBlahController();
        controller.ControllerContext = new ControllerContext()
        {
            Controller = controller,RequestContext = new RequestContext(new MockHttpContext(),new RouteData()),};
    };
}

进一步说明这里是使用模拟会话的测试(MSpec世界的规范):

[Subject("ACCOUNT: Retrieve Password")]
public class retrieve_password_displays_retrieve_password2_page_on_success : BlahBlahControllerContext
{
    static ActionResult result;
    static RetrievePasswordModel model;

    Establish context = () =>
    {
        model = new RetrievePasswordModel()
        {
            UserName = "Mike"
        };
    };

    Because of = () =>
    {
        result = controller.RetrievePassword(model);
    };

    It should_return_a_RedirectToRouteResult = () => 
    { 
        result.is_a_redirect_to_route_and().action_name().ShouldEqual("RetrievePassword2"); 
    };

    It session_should_contain_UN_value = () =>
    {
        controller.HttpContext.Session["UN"].ShouldEqual("Mike");
    };

    It session_should_contain_PQ_value = () =>
    {
        controller.HttpContext.Session["PQ"].ShouldEqual("Question");
    };
}

我意识到这不使用犀牛模型.我希望它能够说明原则,读者可以将其用于具体的工具和方法.

原文链接:https://www.f2er.com/aspnet/249073.html

猜你在找的asp.Net相关文章