gpt4 book ai didi

asp.net - 在 ASP.net 中模拟 HttpSessionState 以进行 nunit 测试

转载 作者:行者123 更新时间:2023-12-04 01:47:50 25 4
gpt4 key购买 nike

我已经看到很多关于 HttpSessionState 和 asp.net MVC 的讨论。
我正在尝试为 asp.net 应用程序编写测试,并想知道是否可以模拟 HttpSessionState,如果可以,如何模拟?

我目前正在使用 Rhino Mocks 和 Nunit

最佳答案

吉尔伯特,

也许我对你来说太晚了。我正在使用 MSpec,但我认为这些概念是相似的。我需要在被测 Controller 中模拟 HttpContext 的几个组件。

我从以下这些类开始模拟 HttpContextBase 中必要的(出于我的目的)组件。我只覆盖了类(class)中必要的部分。您的需求会因 Controller 中所需的模拟而异。一旦你理解了模式,就可以很容易地根据需要添加模拟。

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 http://stackoverflow.com/questions/524457/how-do-you-mock-the-session-object-collection-using-moq
// from http://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 上下文以使用模拟 (MSpec)。这是为 Controller 上的实际测试设置的(测试派生自此类)
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()),
};
};
}

为了进一步说明这里是使用模拟 session 的测试(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");
};
}

我意识到这不使用 Rhino Mocks。我希望它说明了原理,读者可以将其应用到他们的特定工具和方法中。

关于asp.net - 在 ASP.net 中模拟 HttpSessionState 以进行 nunit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5460531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com